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 class be defined inside an interface?
What is string variable?
How does the garbage collector works in java?
What is a data structure java?
What is the difference between Error, defect,fault, failure and mistake?
How does arrays sort work in java?
Which are the two subclasses under exception class?
What is the purpose of skeleton and stub?
What is remote method invocation (rmi)?
What is high level language in computer?
Explain some best practices you would apply while using collection in java?
If we don’t want some of the fields not to serialize how to do that?
Are private methods final?
How do you change an int to a string?
can used Protected Class outside Function.?