• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Method accessing Non Static Method  RSS feed

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code run perfectly.

But the static method is acessing a non static method which was supposed to be not possible.

Is it a syntax trick?Any Explanation to this magic?
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav singh wrote:But the static method is acessing a non static method which was supposed to be not possible.

You misunderstand. It's not possible to directly call a non-static method from a static context. But you have created an instance on which to call the instance method, which is fine.

Remove the "ref" instance and see what happens.
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:

But you have created an instance on which to call the instance method, which is fine.



Why is it when i create an instance it is fine?Any theory or rule applying here?Also If i remove static from line 4 things got wrong.
 
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. The rule is that you can call instance methods of the object you are referencing with ref.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Raghav.

Long story short, how i understand it : static is used to call/get resources (methods/fields/parameters) BEFORE class is instantiated.
Other words : you do not need to call new Test() on your class before you can use elemts you`ve marked as static.

From said above , there are rules to follow : all elements that you mark static has to be independent from class instantiation, i.e. they should be able to survive without non-static elements of your class.
So , if you create static method sum() that tries to get , by its own initiative, some input from any non-static component it will fail to compile and Eclipse or any developer tool you use, will mark this as red to emphasize the issue.

Another evidence of static feature is "main()". In java docs there are strict requirements to main() , so it can be an entry point to the program. One of those requirements for it , is to be static , cause it HAS to be available and operational before any other things were instantiated.

Here is example how main() is awailable before anything because of its static non-access modifier. Running this code you`ll hit StackOverflow cause you recursively call static main(), while calling static main() and all this mess happens without any test3 class instance.



Anyway you should put some effort to read Java tutorials, guys posted above. There are a lot of fundamentall stuff "just for remeber" and without those you`ll not be able to successfuly go forward.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav singh wrote:Why is it when i create an instance it is fine? Any theory or rule applying here?

It's pretty much the foundation of the language. As Bear already stated, non-static methods are called "instance" methods, because they need an instance (or an object) to run on.

You could say that it's actually static methods that are the exception, not instance ones; which is why you have to add the static qualifier.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!