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 |
Explain the difference between treeset and treemap in java?
What modifiers are used for interface declaration?
What is floating data type?
what is meant by Garbage collection?
Why does java have two ways to create child threads? Which way is better?
How many bytes are there?
What does index mean in java?
What is a instance variable in java?
What is a class component?
what is the reason behind non static method cannot be referenced from a static Context?
what is jdk1.5 features?
write a program to create an vector with string(add,remove) operation.and value should be enter through keyboard.