Go to Post I never cease to be amazed at the creative engineering employed by some teams. - Karthik [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

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 13-01-2011, 12:40
correnos correnos is offline
Registered User
FRC #2342
 
Join Date: Jan 2010
Location: Nashua, NH
Posts: 6
correnos is an unknown quantity at this point
Re: Working with java classes

Quote:
Originally Posted by eagle33199 View Post
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!
With most Java programs, creating an object from a class is the advisable path. However, I am unaware of how to accomplish this from the setup used by wpilibj. It is configured on the JVM of the cRIO to run the main class, and if there is a way to tell it to work with an instance of said class instead of the class itself I have not found it yet.
  #2   Spotlight this post!  
Unread 13-01-2011, 13:07
omalleyj omalleyj is offline
Registered User
AKA: Jim O'Malley
FRC #1279 (Cold Fusion)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2008
Location: New Jersey
Posts: 132
omalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to behold
Re: Working with java classes

Quote:
Originally Posted by correnos View Post
With most Java programs, creating an object from a class is the advisable path. However, I am unaware of how to accomplish this from the setup used by wpilibj. It is configured on the JVM of the cRIO to run the main class, and if there is a way to tell it to work with an instance of said class instead of the class itself I have not found it yet.
I think you may be misinterpreting what is happening. When you create a class from SimpleRobot (for instance), you also create (generally) a constructor for it. Your class is instantiated, its constructor is called, and at some point the autonomous() and operatorControl() methods are called.

Unlike a normal desktop app, you are not running the show, the overarching layer is instantiating your robot class and running specific methods at appropriate times.
  #3   Spotlight this post!  
Unread 13-01-2011, 13:07
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

Yes, the robot starts out running the main, and that is always static. It's the same way with java programs - you create a static main method that's responsible for launching everything else. Within that static main method, create your objects for everything else you want to do. For example:

Code:
public static void main(string[] args)
{
    RobotCode myCode = new RobotCode();
    while (myCode.isEnabled())
    {
        //do stuff
    }
}
  #4   Spotlight this post!  
Unread 13-01-2011, 20:52
correnos correnos is offline
Registered User
FRC #2342
 
Join Date: Jan 2010
Location: Nashua, NH
Posts: 6
correnos is an unknown quantity at this point
Re: Working with java classes

I see the problem. I'm using the iterative robot template. It implements the main() method already.
Closed Thread


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