• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

non-static method invocation in static method

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can I not call a non-static method in a static method without object creation? Can anyone please explain to me why that does not work
 
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever we create a method it is loaded in JVM(Java Virtual Machine) in two main ways.

1. Static Methods 2. Non-Static Methods

1. Static Methods

In JVM, there is special memory allocated for Static members of a class (methods,variables,blocks,etc. which are marked as "static"). Whenever the class containing these methods is loaded in JVM, automatically these members are also loaded inside the JVM. So they are now existing in the system.

1. Non-Static Methods

In JVM, the members of a class which are not static , are not loaded automatically inside the JVM, but to load those we need to create object of that class. Once the object is created these methods are now existing in the system. Without object creation they do not exists.


Now you can imagine why it is needed an object to call a non static method.

As you can see, without object (non-static) methods or variables do not exists, then How can you call something which is not Existing.

But for Static members, they are loaded with class itself, not with Object, So you can call them without Object.

You can't call something that doesn't exist. Since you haven't created an object, the non-static method doesn't exist yet. A static method (by definition) always exists.



Static methods belong to the class; non-static methods belong to instances of the class


 
Marshal
Posts: 79151
377
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You should avoid the keyword static as much as possible. It has its uses but it is often used incorrectly. You should write a main method with one statement in:-
new Foo(...).run(...);
… and the run method should start your application.
If you are in a static context, you cannot access non‑static members of the class. If you call the method method1() from a static method, and there are two instances of the class, which instance should you call it on? Don't know. That is why you must not access non‑static members from a static method. What if there are no instances of the class? How can you call that non‑static method?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
M Shu,
Your post was moved to a new topic.
(This informational message will self destruct in two days)

Okay, this whole side discussion has been separated, as it seems to be more on how to answer a question than about the question itself. Additionally, I disabled the self destruct to maintain the forward link ... just in case the context is important.

Henry
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M Shu wrote:
1. Static Methods

In JVM, there is special memory allocated for Static members of a class (methods,variables,blocks,etc. which are marked as "static"). Whenever the class containing these methods is loaded in JVM, automatically these members are also loaded inside the JVM. So they are now existing in the system.

1. Non-Static Methods

In JVM, the members of a class which are not static , are not loaded automatically inside the JVM, but to load those we need to create object of that class. Once the object is created these methods are now existing in the system. Without object creation they do not exists.



This is completely wrong.
The class is loaded when the class is first accessed.
At that point all methods are loaded (they are part of the class, not an object), whether static or not, and all static data is loaded.
The only thing created memory-wise for an object of a class is its member data when it is instantiated.
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic