Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding static and main

 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin explained it well, and The Java Tutorial on Sun's Java website also explains what 'static' means:

Understanding Instance and Class Members
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic