Go to Post The world isn't made out of Nerf, you will get scrapes and bruses, but you will get back up and continue on. If you watch out you can avoid the sharp edges. - Joe Matt [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Spotlight this post!  
Unread 13-01-2011, 11:54
Jon Stratis's Avatar
Jon Stratis Jon Stratis is offline
Electrical/Programming Mentor
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2007
Rookie Year: 2006
Location: Minnesota
Posts: 3,753
Jon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond repute
Re: Working with java classes

Your problem here is thinking of them as classes with methods instead of objects.

For most work in Java, you'll create objects from your classes. For example:
RobotCode myCode = new RobotCode();

Then you do operations on that object, myCode. You'll access your functions with something like:
myCode.isEnabled();

This gives you flexibility in creating multiple objects - for example, you'll probably want to create a new object for each of the Jaguars you have. When you create it, you tell it where the Jaguar is hooked up, and don't have to worry about specifying that location ever again.

Outside of all this, Java recognizes that sometimes you don't really need/want to specify a new object when accessing a class, class methods, or class variables. For this, they have the static context. If you declare it static, it can be accessed straight from the class itself. This is called a singleton, as there's only a single instance of it you can use, and everyone who calls it gets the same instance of that class. You need to be very careful with this, as any class variables that are modified in one location, will read that modified value in another, sometimes with horrible results!

So to access your isEnabled() class in a static way, you need to declare it as follows:

public static Boolean isEnabled()
{
...
}

I'm assuming here that the return type is Boolean, and that you want it to be publicly available (as opposed to private or protected).

Likely you'll want to go with the first method, and not do too much static code... at least, not until you really understand the difference and the impact!
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 03:49.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi