adbrite adds

Your Ad Here

Java J2EE Best Answer

Read all the question and answer in the post and success the Interview.


search for Interview questions and job

Friday, August 22, 2008

Some questions of Static in java

Static in java

1. Why is the main method static?

So that it can be invoked without creating an instance of that Class.

2. What is the difference between static and non-static variables?

A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.

3. What is the difference between a static and a non-static inner class?

A non-static inner class may have object instances that are associated with instances of the class's outer class. A static inner class does not have any object instances.

4. Where can static modifiers are used?

Static modifiers applied to variables, methods and a block of code, static methods and variables are not associated with any instance of class.

5. When are the static variables loaded into the memory?

During the class load time

6. When are the non static variables loaded into the memory?

They are loaded just before the constructor is called.

7. How can you reference static variables?

Via reference to any instance of the class
Computer is a class name.
We call the static variable , static method as
Computer.harddisk harddisk is variable of the class Computer
Computer.compute() computer() is a method of the class Computer


8. Can static method use non static features of there class

No, they are not allowed to use non static features of the class; they can only call static methods and can use static data.

9. What is static initializer code?

A class can have a block of initializer code that is simply surrounded by curly braces and labeled as static e.g.
public class Demo{
static int =10;
static{
System.out.println(“Hello world’);
}
}

And this code is executed exactly once at the time of class Load

10. Can inner classes be static?

Yes, inner classes can be static, but they cannot access the non static data of the outer classes.

11. What does it mean that a method or field is "static"?

A: 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.

12. Can main method be declared final?

Yes, the main method can be declared final, in addition to being public static.

13. What are the static fields & static Methods ?

If a field or method defined as a static, there is only one copy for entire class, rather than one copy for each instance of class. static method cannot accecss non-static field or call non-static method. Static method can’t Override.

14. Why are the methods of the Math class static?

So they can be invoked as if they are a mathematical code library.

15. When are static and non static variables of the class initialized?

The static variables are initialized when the class is loaded. Non static variables are initialized just before the constructor is called

No comments:

Study JAVA J2EE

Read EJB Session Bean Guide post and do EJB with a hour BEST OF LUCK CONTACT 9942291292

click to get job