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 / 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 |
What is variable length arguments in java?
how to execute main()
What is constructor in java ?
What is mean by encoding?
How many types of memory areas are allocated by JVM in java?
What is the purpose of checked and unchecked exceptions in JAVA?
How to invoke external process in java.
Are the imports checked for validity at compile time? Will the code containing an import such as java.lang.abcd compile?
Can a class have 2 constructors?
What is sorting in java?
What is the size of string?
How to split a string in java?