as we know a static method could access static data and
static method only. then how could main method call the
object of aclass which is not static and other non static data



as we know a static method could access static data and static method only. then how could main met..

Answer / ramya

though main method is static, it can access non-static
(instance) variabels or methods by creating an instance of
the class in which the variables and methods are..
Example:
class LessonTwoB {

String text = "I'm a Simple Program";
static String text2 = "I'm static text";

String getText(){
return text;
}

String getStaticText(){
return text2;
}

public static void main(String[] args){
LessonTwoB progInstance = new LessonTwoB();
String retrievedText = progInstance.getText();
String retrievedStaticText =
progInstance.getStaticText();
System.out.println(retrievedText);
System.out.println(retrievedStaticText);
}
}

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More Core Java Interview Questions

How are Java source code files named?

3 Answers  


What is a priority queue java?

0 Answers  


What do you mean by access modifier?

0 Answers  


Can a class be subclass of itself?

4 Answers  


what is the difference between a java object reference and c++ pointer?

4 Answers  






What is variable and its types?

0 Answers  


How are the elements of a gridbaglayout organized in java programming?

0 Answers  


Is .net better than java?

0 Answers  


When does Exception occurs?

3 Answers  


Can try statements be nested?

3 Answers   Wipro,


What are jee technologies?

0 Answers  


Which are thin and thicK wrapper Class's in Java?

1 Answers  


Categories