Go to Post It's not a rendering mistake, it's a design challenge! - Eric Scheuing [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, 11:28
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
Working with java classes

Hello,
I'm a programmer on an FRC team trying to work with Java. In previous years, we've programmed in C++, and we're making the switch this year. My question is on calling methods in the main class from other classes. Specifically, I'm trying to setup a separate thread for vacuum control, and would like to check whether the robot is enabled. Unfortunately, I cannot check RobotCode.isEnabled(), with the error "non-static method isEnabled() cannot be referenced from a static context". The same error pops up when attempting to access the Jaguar controlling the vacuum or the joystick's trigger. I understand why the error occurs, as these attempts access the class as opposed to its instance, however I do not know how I would work around this. Declaring the joystick and jaguar as static allow access to those, but I still cannot check isEnabled(). How would I check it? I could have a loop in RobotCode updating a static variable, but is there a better solution?

EDIT: I found that placing the vacuum class within the main class fixed the issue. It worked, but if anyone has a cleaner solution I would very much like to hear it.

Last edited by correnos : 13-01-2011 at 11:38.
  #2   Spotlight this post!  
Unread 13-01-2011, 11:54
Jon Stratis's Avatar
Jon Stratis Jon Stratis is offline
Mentor, LRI, MN RPC
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2007
Rookie Year: 2006
Location: Minnesota
Posts: 3,816
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!
  #3   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.
  #4   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.
  #5   Spotlight this post!  
Unread 13-01-2011, 13:07
Jon Stratis's Avatar
Jon Stratis Jon Stratis is offline
Mentor, LRI, MN RPC
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2007
Rookie Year: 2006
Location: Minnesota
Posts: 3,816
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
    }
}
  #6   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 23:37.

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