Tuesday, November 29, 2011
Core Java Interview Questions :What does it mean that a method or field is ?static??
Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class. Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That?s how library methods like System.out.println() work. out is a static field in the java.lang.System class.:static key word is prefixed with field are method that is not issu in both case the memory is first allocated and aso to break the oops concept rule:Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class. Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That?s how library methods like System.out.println() work. out is a static field in the java.lang.System class.:Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class. Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That?s how library methods like System.out.println() work. out is a static field in the java.lang.System class.:Basically, static methods will be written in an application when user need to do static logic irrespective of the object wise coding. Similarly, static methods will be accessed at class level.:static members are called class members.class members are initialized only once ,at the time the class is loaded in to jvm.class members have only one copy for particular application and same copy used by all instances of that class.:All of the basic, we make the method static when we want to access that method before the class is instantiated. (i.e. calling the method without any object) and so is the main() method static.:If a member (ie methods & Fields) is declared as static then those things are belong to Class rather than Instances. You can access these static fields by using Class name.These members are also known as class members.:The static method or the fields can be instantiated only once per class. Any changes made to the static method will reflect within the scope. More over the the methods or the fields cannot be inherited.:static data can be accessed by only static members because this we can access any static member of other class in "public static void main()" function.:variables and methods accessed with out creating the instance of a class:static methods and variables are not object specific. Only one copy of them is there for all the objects of that class.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment