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 we override data members in java?
What is the main purpose of serialization in java?
What is use of static in java?
What are the types of arrays in java?
These static constructors are correct ? class A { statc intA() { } static A(int x,int y) { } static A(int x) { } }
What is the size of integer?
Is it possible to use string in the switch case?
What is the difference between compiler and jvm?
Can we create constructor in abstract class ?
What are the two environment variables that must be set in order to run any java programs?
Can we create object of static class?
What is the flag in java?
Under what conditions is an object’s finalize() method invoked by the garbage collector?
What is the use of 'super' keyword inside a constructor?
When can you say a graph to be a tree?