aspose file tools*
The moose likes Beginning Java and the fly likes HF Java Chp 10 - Final & Static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HF Java Chp 10 - Final & Static" Watch "HF Java Chp 10 - Final & Static" New topic
Author

HF Java Chp 10 - Final & Static

Cyndie Enfinger
Greenhorn

Joined: Aug 11, 2009
Posts: 4
On page 285 in Chapter 10 there is a Sharpen Your Pencil Excercise that gives two examples of a public class with a method that takes a final argument:

public void go(final int x)...

Does this mean that go() can be called many times from different class instances passing different values as x, but x cannot by changed by anything within go()?

public static go(final int x)...

Does this one mean that go() can be called many times as long as the value passed to it is static and it is called by the class name?

There are no answers and I'm not taking an instructor lead class, so I'm a little unclear if I understand these correctly.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
This simply means that:



will make the compiler choke.
You cannot reassign the variable inside the method.

That has nothing to do with the static context of the method.


JDBCSupport - An easy to use, light-weight JDBC framework -
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39803
    
  28
Welcome to JavaRanch

As Sebastian Janisch has already said, the final modifier on the parameter means the parameter may not be re-assigned in that method. It is probably a good idea to make parameters final, but it doesn't appear to be obvious from the documentation comments whether a parameter is final or not.
Cyndie Enfinger
Greenhorn

Joined: Aug 11, 2009
Posts: 4
Okay, thanks to you both.

So it would be fair to say the first one could be called as follows:

Foo myFoo = new Foo();
myFoo.go(1);
myFoo.go(2);

but, the second one could only be called like this?

static int x = 5;
static int y = 6
Foo.go(x);
Foo.go(y);
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Err no,

The code is fine, but i feel you are missing out on the static context:

first one :


Second one:



There is nothing wrong with the code you wrote, i am merely remarking on the concept that x and y are static in your code,
they need not be static

There is no rule that says you should pass only static variables to static method.
Within the static method, you can refer to only static content.


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

when you apply a non access modifier for a method then it holds for the method and not to its parameters. When you give a static to a method means its a static member of the class therefore implying that it doesn't need an instance of the class to access it. But the arguments passed can be anything. When you say final means its the end....in java terms that particular variable once assigned cannot be reassigned and for a method it cannot be overriden. In a method if it has a final parameter means the variable which is passed as argument to the parameter cannot be changed.

Cheers


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39803
    
  28
Nitish Bangera wrote: . . . When you say final means its the end....in java terms . . .
. . . and "final" about a class means it cannot be inherited from.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Hehe yeah thanks for the clean up
Cyndie Enfinger
Greenhorn

Joined: Aug 11, 2009
Posts: 4
I misunderstood on being able to pass non-static variables to a static method. The HF book says "A static method can't access a non-static variable" and I guess I took that to mean you couldn't pass one either. This makes more sense and I appreciate the clarification.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HF Java Chp 10 - Final & Static