Results 1 to 9 of 9
- 12-01-2010, 08:30 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
Passing final parameter from a class
How would I pass a final parameter from a class into a method? Example:
Java Code:class Vehicle{ private direction = 0; public static final int TURN_LEFT = -90; public static final int TURN_RIGHT = 90; public void turn (final int TURN_LEFT){ // This is probably wrong... How to fix this? direction = direction + TURN_LEFT; } }
- 12-01-2010, 09:11 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
How would I pass a final parameter from a class into a method?
I'm not completely sure what you are trying to do. Perhaps you mean something like this:
Java Code:class Vehicle { private int direction = 0; public static final int TURN_LEFT = -90; public static final int TURN_RIGHT = 90; public void turn (int amount) { //direction = direction + amount; // better as direction += amount; } // the following code would probably be in another method // in another class public static void main(String[] args) { Vehicle test = new Vehicle(); System.out.println("test created, direction=" + test.direction); test.turn(TURN_LEFT); System.out.println("test turned, direction=" + test.direction); test.turn(TURN_RIGHT); test.turn(TURN_RIGHT); test.turn(TURN_RIGHT); System.out.println("finally, direction=" + test.direction); } }
Note that what the method ends up with is the value of the parameter. SO it doesn't really matter whether a variable used as a parameter is final or not - the method uses the value it is given in the same way.
----------------------
If you are not trying to do this (use final variables as parameters), say.
- 12-01-2010, 09:15 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
This was kind of an exercise on overloading. I need to create two methods, one where turn accepts any float value and another where turn accepts either of the two final parameters TURN_LEFT or TURN_RIGHT declared in the class Vehicle. I suppose this seems a little weird.. but that was the question.
- 12-01-2010, 09:48 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
To be specific, the question is exactly as such:
Add two turn methods to Vehicle: one that takes a number of degrees to turn and one that takes either of the constants Vehicle.TURN_LEFT or Vehicle.TURN_RIGHT.
- 12-01-2010, 10:33 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Since the methods are overloaded you will need a different type for TURN_LEFT and TURN_RIGHT. That is, these things can't be ints because if they were the compiler would have no way of telling which turn() method to use.
Java provides enums for just this sort of situation. Unlike the "static final whatevers" that we used to use an enum is a type and, so, can be used in overloaded methods (they have other advantages too). They are described in the EnumTypes section of Oracle's Tutorial and the 1.5 Guide.
Consider something like:
Java Code:import java.awt.Color; class Vehicle{ private Color color = Color.BLACK; public enum StandardColor { RED(Color.RED), BLUE(Color.BLUE); Color color; StandardColor(Color c) { color = c; } } public void paint(Color c) { System.out.println("Nonstandard!"); color = c; } public void paint(StandardColor c) { System.out.println("Standard!"); color = c.color; } // the following code would probably be in another method // in another class public static void main(String[] args) { Vehicle test = new Vehicle(); System.out.println("test created, color=" + test.color); test.paint(Color.GREEN); System.out.println("test painted=" + test.color); test.paint(StandardColor.BLUE); System.out.println("test painted=" + test.color); } }
You could make Vehicle.TURN_XXX some other type (like a String) and overload the methods that way. But it seems to me that enums would be neater.
- 12-02-2010, 08:19 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
Thanks pbrockway2, followed your advice and did something like this
Is there no other way of writing in such that I don't have to include the .FixedAngle?Java Code:// In Vehicle.java public class Vehicle{ private float direction = 0; public enum FixedAngle{ TURN_LEFT(-90.0),TURN_RIGHT(90.0); private double turnAngle; FixedAngle(double turnAngle){ this.turnAngle = turnAngle; } double value(){ return turnAngle; } } // My overloaded turn methods public void turn(float turnAngle){ direction = direction + turnAngle; } public void turn(FixedAngle turnAngle){ double z = turnAngle.value(); } ... } // In TurnVehicle.java public class TurnVehicle{ public static void main(String[] args){ Vehicle Z = new Vehicle(90,90,"lame_driver"); System.out.println("Initial direction: " + Z.getDirection()); Z.turn(-100); System.out.println("Turning -100: " + Z.getDirection()); Z.turn(Vehicle.FixedAngle.TURN_LEFT); System.out.println("Turning Left: " + Z.getDirection()); Z.turn(Vehicle.FixedAngle.TURN_RIGHT); System.out.println("Turning Right: " + Z.getDirection()); } }
As a side question, in the enum I could only use doubles but not floats, got an error message from the compiler (javac) as such (the arrows are misplaced, should be pointing at each '('):
Vehicle.java:14: cannot find symbol
symbol : constructor FixedAngle(double)
location: class Vehicle.FixedAngle
TURN_LEFT(-90.0),TURN_RIGHT(90.0);
^
Vehicle.java:14: cannot find symbol
symbol : constructor FixedAngle(double)
location: class Vehicle.FixedAngle
TURN_LEFT(-90.0),TURN_RIGHT(90.0);
^
2 errorsLast edited by ianyappy; 12-02-2010 at 10:06 AM.
- 12-02-2010, 06:50 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
If can use floats, but the constructor and the values of the enums must agree:
Java Code:public enum FixedAngle{ TURN_LEFT(-90.0f),TURN_RIGHT(90.0f); private float turnAngle; FixedAngle(float turnAngle){ this.turnAngle = turnAngle; // etc
Floats are relatively little used in Java. (Also you were using ints at the start of the thread.)
Is there no other way of writing in such that I don't have to include the .FixedAngle?
If your classes are in packages you can use an "import static" and refer to the fixed angles in other classes as TURN_XXX. Or you could have a final static variable variable in your Vehicle class:
Java Code:public class Vehicle{ private float direction = 0; public enum FixedAngle{ TURN_LEFT(-90.0),TURN_RIGHT(90.0); private double turnAngle; FixedAngle(double turnAngle){ this.turnAngle = turnAngle; } double value(){ return turnAngle; } } public static final FixedAngle TURN_LEFT = FixedAngle.TURN_LEFT; // ...
The fixed angle TURN_LEFT can now be used the way you were trying to use an int at the start of the thread but now it is a different type and, so, can be used for overloading.
--------------------
Check you get the right answers in main(). Your turn(FixedAngle) method doesn't seem to be changing the value of direction.
- 12-02-2010, 07:04 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
Ah, ok understood that all. Thanks for the help!
- 12-02-2010, 07:06 PM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Similar Threads
-
Switch Statement/Parameter Passing
By spmooney@hotmail.co.uk in forum NetBeansReplies: 1Last Post: 01-06-2010, 01:50 PM -
passing parameter to a thread
By adammyth in forum Threads and SynchronizationReplies: 1Last Post: 01-02-2010, 07:58 PM -
passing a parameter
By aarthi2learn in forum AWT / SwingReplies: 4Last Post: 12-22-2008, 05:46 AM -
Passing short value as parameter
By javanewbie83 in forum New To JavaReplies: 16Last Post: 07-16-2008, 05:27 AM -
passing an enum type as a parameter ??!
By SCS17 in forum New To JavaReplies: 11Last Post: 07-13-2008, 01:44 PM


LinkBack URL
About LinkBacks

Bookmarks