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

Explain about static imports in java?

0 Answers  


What is the purpose of assert keyword used in jdk1.4.x?

0 Answers  


Differentiate between == and equals().

0 Answers  


Is hashset sorted in java?

0 Answers  


how to pass the parameters to applets?

1 Answers  






Nullpointer exception is a very common exception. Why is it not made as a checked exception?

2 Answers   Ness Technologies,


What is Three tier architecture. Can anyone explain with a Ordinary web application Project? (JSP,Servlets,JAVA,DAO) ?

3 Answers   TCS,


What are disadvantages of java?

0 Answers  


What does n mean in java?

0 Answers  


What is the difference between array list and vector in java?

0 Answers  


Hi ,i convert contrller as jSp And presentation as servlet ...will it do? if so what are advantage and idsadvantages

2 Answers   IBM,


Explain about interthread communication and how it takes place in java?

0 Answers  


Categories