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
Can a java program have 2 main methods?
Can we sort set in java?
Is there a case when finally will not execute?
What is the protected method modifier?
Tell me about different OOPS concepts.
What about main thread in java?
Write a program to show whether a graph is a tree or not using adjacency matrix.
What is the difference between sleep and wait in java?
What is string variable?
How can you write a loop indefinitely in java programming?
How do you use compareto method?
What is meant by 'Class access modifiers'?
why are wait(), notify() and notifyall() methods defined in the object class? : Java thread
How to find the index of the largest number in an arraylist java?
What is object cloning in Java?