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

Without creating a new object, How you can retrieve a String or other object?

1 Answers  


Explain the key functions of data binding?

0 Answers   Akamai Technologies, Aspire, Impetus, Infogain, Tavant Technologies, Virtusa,


What is a databasemetadata?

0 Answers  


how to run servlet program between two computer through the internet ?

2 Answers   Kiran Prakashan, TCS,


What is the purpose of return statement?

0 Answers  


Can an abstract class be final?

10 Answers  


In Serialization, whether you will use Static variables?

3 Answers   HCL,


How will you measure that int takes up only 32 bits in memory?

2 Answers   Rolta,


What is HashTable?

4 Answers  


Do I need java on my computer?

0 Answers  


What are parsing rules?

0 Answers  


Difference between local and global transaction ?

1 Answers   Accenture, iFlex,


Categories