• Post Reply Bookmark Topic Watch Topic
  • New Topic

non-static method invocation in static method  RSS feed

 
Poojitha Polusani
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
M Shu
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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

 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 3
  • Mark post as helpful
  • send pies
  • 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?
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!