This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

core java, static  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in java non-static fields can not be accessed in a static field.
But if we create an object of class type in static field and then call non-static field in it, it works fine as in following program ( Ultimately this is what we do in static main method). So is right to say that practically its not impossible to call non-static fields in static fields.

//Static.java
public class Static
{

static void stat()
{
Static st1 = new Static();
st1.stat1();
}

void stat1()
{
System.out.println("You can call me within static method with the help of class object");

}


public static void main(String args[])
{
Static st1 = new Static();

st1.stat();

}
}
 
Author and ninkuma
Marshal
Posts: 66797
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.
 
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you're *not* calling a non-static method from a static: you're instantiating an instance and calling a method on that instance.
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you call a static method from another static method? Can we call a non-static method like that?
 
Marshal
Posts: 60075
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:How do you call a static method from another static method? Can we call a non-static method like that?

Like this:You can of course call an instance method similarly, but only inside another instance method (or initialiser block or constructor, but those may require special precautions).
 
Ammy Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Newton wrote:But you're *not* calling a non-static method from a static: you're instantiating an instance and calling a method on that instance.



Dear David Newton, thanks for your valuable reply.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Abimaran Kugathasan wrote:How do you call a static method from another static method? Can we call a non-static method like that?

Like this:You can of course call an instance method similarly, but only inside another instance method (or initialiser block or constructor, but those may require special precautions).



Yup, that's I asked!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!