• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static and Non-static plz help  RSS feed

 
Imran Kazi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not getting the idea of "non-static method cannot be referenced

from static context".

before that chapter about static method and variable i've written and

seen many codes where an instance calls a non-static method from inside

the main() method. main() is a static method, right? so how can i call

a non-static method from inside main()? I think something is wrong

about my concept, but can't figure it out...plz help


below i'm giving two codes: one where i've used static method. in the

ohter one i've used non-static method. but both are working fine. plz

explain me why. these whole things about static nad non-static is the

most complicated upto what i've studied in java.

-------------------
(1) here Shout() is non-static

class WhyStatic{

public void Shout(){

System.out.println("I'm not static");}
}

class StaticDrive{

public static void main(String[] args){

WhyStatic ref= new WhyStatic();
ref.Shout();
}
}

---------------------
(2)here Shout() is static.


class WhyStatic2{

static void Shout(){

System.out.println("I'm not static");}
}

class StaticDrive2{

public static void main(String[] args){

WhyStatic2 ref= new WhyStatic2();
ref.Shout();
}
}

--------------------

So, what's the difference? plz help...
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember Static means Class Level and non static means Object Level.

Static variables are created and initialized before non static variables that means static variables are availiable and are in the context of non static. Therefore One can refer static members using object of the Class also.

And As non static are not created before static members ...they do not exist in the context of static members. Therefore non Static members can not be referenced from static directly without making object.

Then, regarding your Query....

Q. How main method(static) is able to access both static and non static method
A: If you notice...you have made object before accessing non static method from main. Without making object, you can not access it.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my take on static and non-static is this....

say you have the class..


i guess that's right, hope this helps a lil...

Justin
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In both of your examples, you are creating an instance of a class and calling a method with the instance.

It may help to think of a class definition like a template, where the non-static members are marked "to be filled in when a class definition is created."

The static variables in a class definition are initialized when the class definition is initialized. But the non-static members are only initialized when an instance of a class is created.

So in the following example.



it makes no sense to try to print out the value of the num, because no instance of the class has been created. num is simply a variable which will be initialized when an instance of the class is created.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!