• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Ranch Hand
Posts: 54
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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++

 
Ranch Hand
Posts: 35
Hibernate jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic