• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Doubt regarding static and main

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Marshal
Posts: 79962
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic