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 |
How can i add a button in applet in java ???
Can a main method be overloaded?
What is a values collection view ?
What is meant by inheritance and what are its advantages?
What is the need of "creating and throwing an UserdefinedException" when the "Exception" class is already available?
Why do we need variables?
What is lambda expression in java?
What are the methods to rectify ambiguities in the interfaces in JAVA?
What is a line separator in java?
Why do we need autoboxing in java?
Is java map thread safe?
What is a bubble sort in java?