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

Can set contain duplicates?

0 Answers  


What is JVM ? use of JVM?

6 Answers   BVC, Tech Mahindra,


how session will be expired ?

4 Answers   Satyam,


String and stringbuffer both represent string objects. Can we compare string and stringbuffer in java?

0 Answers  


I have 100 records in a table with two rows. I need to display 10 records per page like Google Search. I need only the Logic(Pagination) in Pure Java. No JSP and all..Thanks in Advance...

2 Answers   Cybernet,






Can we serialize arraylist in java?

0 Answers  


Urgent requirement of JAVA/.NET/QA in a CMMI level3 MNC for Pune location.

0 Answers  


What is the use of math abs in java?

0 Answers  


What is this keyword in java?

0 Answers  


What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. Import java.net.* Versus import java.net.socket)?

0 Answers  


What are different types of expressions?

0 Answers  


What are controls and their different types in awt?

0 Answers  


Categories