• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

non-static method can't be referenced from static context

 
Himanshu V Singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


But Method can be accessed via object of the class, why is it so, please guide me with a brief explanation ?
 
Jared Malcolm
Ranch Hand
Posts: 54
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You first have to instantiate the class TryCatchTest in order to access a non static member. Just the rules of Java....a static member can't access a non static one. so....



For me though I generally just make a constructor to take the place of the run method. Just gets rid of some unneeded code (in my mind anyway).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your example, you haven't created an instance of your class. Therefore, "[The] Method can be accessed via object of the class" is irrelevant.
 
Himanshu V Singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jared Malcolm wrote:You first have to instantiate the class TryCatchTest in order to access a non static member. Just the rules of Java....a static member can't access a non static one. so....



For me though I generally just make a constructor to take the place of the run method. Just gets rid of some unneeded code (in my mind anyway).





But within same class, cant i access methods with out using object of class like c++

 
Rupesh Mhatre
Ranch Hand
Posts: 35
Eclipse IDE Hibernate jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu V Singh wrote:

But Method can be accessed via object of the class, why is it so, please guide me with a brief explanation ?


It has very simple reason static members are class level members and you don't need instance for that and non static members are instance specific so they need instance to get called so in your case if run method had been static method then you could use it without creating any instance like TryCatchTest.run();. Now as your run method is not static so it is a instance specific method so when you call it from static method i.e. main method you get compiler error saying non-static method can't be referenced from static context. I guess this will help.
 
Jared Malcolm
Ranch Hand
Posts: 54
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rupesh Mhatre wrote:TryCatchTest.run();


If the run method had been static there would be no reason to even put the TryCatchTest in there........could have simply put run();
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu V Singh wrote:
But within same class, cant i access methods with out using object of class like c++




Please get rid of this idea of comparing c++ and java.Trust me they are way different although it might seem they have loads of similarities, and are OOP languages!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic