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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding static and main" Watch "Doubt regarding static and main" New topic
Author

Doubt regarding static and main

Kaush Kane
Ranch Hand

Joined: May 22, 2006
Posts: 37
The static functions can access only static varibales and can call other static functions.
The above being the definition of static:
How can main access non-static functions of a class via objects??
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
as you say - "via object".

if you want to access anything non static from inside the main method - no way but creating a instance and call the method / field on this instance...

:-)

jan
Kaush Kane
Ranch Hand

Joined: May 22, 2006
Posts: 37
if you want to access anything non static from inside the main method - no way but creating a instance and call the method / field on this instance


Does this hold true for static methods other than main??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Does this hold true for static methods other than main??


Yes.

But you ought to have as little as possible inside your main method.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
What you need to understand is that a static member belongs to no instance, but it belongs to the class itself.

On the other hand, instance members are different. There is a copy of them for every instance that you create.

When you are writing code in a static context you must be acquainted that you are not working for an specific instance, but for the class itself, and there is not a notion of an specific instance in that context.

That is why you cannot access instance members, because there is no way to know, within a static context, which specific instance of the class you maybe referring to when you access an instance member.

To illustrate it, see this code:



The static method main cannot access the instance member "name" because it belongs to an specific instance unknown to the static context, in this case under the static context even no instance of the class Jedi exists yet.

However, this restriction does not imply that a static method cannot create instances of objects. For instance:



In this case there is no problem, because the main method is accessing the instance member through the specific instance it belongs to.

The conclusion is, static method cannot access instance members directly, but this does not mean that an static method cannot create new objects. If one of these objects it creates is an instance of its same class, the it could access the instance members of its same class through it.
[ June 06, 2006: Message edited by: Edwin Dalorzo ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Edwin explained it well, and The Java Tutorial on Sun's Java website also explains what 'static' means:

Understanding Instance and Class Members


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt regarding static and main