aspose file tools*
The moose likes Beginning Java and the fly likes static vs non static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static vs non static methods" Watch "static vs non static methods" New topic
Author

static vs non static methods

xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
hi guys,
i been going through the theroy of static vs non static methods.
from what i have read.
static method cant access a non static method in the same class
where else, a non static method able to.
but from this example codes.
i am thinking findAreaVersion1 is a static method as
findAreaVersion2(Square sq) make use of the static method values..

am i wrong in saying this? please enlighten.
i'm sort of confused over this..

Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 279
Both are instance methods in this example. Method findAreaVersion2 is not declared as static and does not need to declared as static in order to work. However, since it does not operate on any instance variables, it probably should be declared static. Otherwise, you will need an existing instance of Square in order to invoke findAreaVersion2.

You are correct in that an instance method may access a static method but not the other way around.
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
Originally posted by Jay Damon:
Both are instance methods in this example. Method findAreaVersion2 is not declared as static and does not need to declared as static in order to work. However, since it does not operate on any instance variables, it probably should be declared static. Otherwise, you will need an existing instance of Square in order to invoke findAreaVersion2.

You are correct in that an instance method may access a static method but not the other way around.


thanks for yr reply and help.
however, what i want to clarify is that whether my understanding of this example is correct by saying
the version2 variable (square sq)has no meaningful values if an instant of square does not exist that why it can be say it's a non static method..
[ September 21, 2007: Message edited by: xiao sean ]
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 279
I'm not sure what you mean by "meaningful values". Reread my post:
it does not operate on any instance variables

Method findAreaVersion2 is an instance method because is not declared with a static keyword. You may refer to it as non-static if you choose but that is not the preferred terminology. Because it is not declared with a static keyword, an instance of Square must exist in order for the method to be invoked.
[ September 21, 2007: Message edited by: Jay Damon ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by xiao sean:
...the version2 variable (square sq)has no meaningful values if an instant of square does not exist that why it can be say it's a non static method...

To be static, a member must be declared static. So the simple answer is that findAreaVersion2 is not static because it has no "static" modifier.

The method findAreaVersion2 does not need anything other than the parameter passed to it, so in that respect, this example could work as a static method. However, there are other things to consider. For example, static methods cannot be overridden, nor can a non-static method be overridden to be static.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
thanks for reply. actually i dont understand how(square sq) appears on verison 2.

should i say that because of this reason, it become a non static method?

please advice,.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by xiao sean:
...actually i dont understand how(square sq) appears on verison 2.

should i say that because of this reason, it become a non static method? ...

That's just an argument to the method. It has nothing to do with whether or not the method is a static.
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
Originally posted by marc weber:

That's just an argument to the method. It has nothing to do with whether or not the method is a static.


sorry i mean the sq.side wasnt declare, and i wasnt sure what is sq.side? isit the reason of why it wasnt a static method?
[ September 22, 2007: Message edited by: xiao sean ]
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
sorry anyone able to explain this to me? i still confuse over this
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
Originally posted by marc weber:

To be static, a member must be declared static. So the simple answer is that findAreaVersion2 is not static because it has no "static" modifier.

The method findAreaVersion2 does not need anything other than the parameter passed to it, so in that respect, this example could work as a static method. However, there are other things to consider. For example, static methods cannot be overridden, nor can a non-static method be overridden to be static.



hi, from my repeated reading of your reply, it seem you are telling me that version2 is a static method too like version1 or?? sorry, i really dont get it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The method is not static because it does not have a "static" modifier.

When the method is called, the argument is an instance of Square, and this instance is assigned to a variable called "sq" that is local to the method. This is specified by the method's argument list inside the parentheses: (Square sq).

So within the method body, "sq.side" references the variable "side" within the instance of Square referenced by "sq."

The arguments passed to a method have nothing to do with it being static or not.
David Sugar
Greenhorn

Joined: Jul 10, 2002
Posts: 2
Originally posted by xiao sean:


to use:


Now, findAreaVersion2 is a static method. All other methods are instance (non-static). Since findAreaVersion2 does not refer to any instance fields, it can be made static.

David
[ November 06, 2007: Message edited by: David Sugar ]
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
Just reading some topics for fun, and that last post sums it up properly.
Of course, I wouldn't bother with that static method when you can use the instance method, nevertheless, its a good question, and worth thinking about if you don't get the whole static/instance thing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static vs non static methods
 
Similar Threads
static Methods vs static variables
Inheritance in a problem...
Point-Circle-Cylinder
A Quadrilateral extend program
Abstract Class