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
Answer Posted / 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 View All Answers
What is difference between throw and throws ?
What does flagged out mean?
What is the maximum size of hashmap in java?
Can I uninstall java?
Does the order of public and static declaration matter in main method?
Can we convert integer to string in java?
What is OOP's Terms with explanation?
What is a concrete classes? Is Java object class is concrete class?
What does index mean in java?
What is constructor and its types?
Explain the importance of import keyword in java?
What are the types of exceptions?
What is operator overloading. Is it is supported in java?
What’s the difference between constructors and other methods?
Is array an object in java?