• Post Reply Bookmark Topic Watch Topic
  • New Topic

core java, static  RSS feed

 
Ammy Singh
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();

}
}
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
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.
 
David Newton
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.
 
Abimaran Kugathasan
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?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • 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!