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



as we know a static method could access static data and static method only. then how could main met..

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

Post New Answer

More Core Java Interview Questions

What are the main features of java?

0 Answers  


what is thread? : Java thread

0 Answers  


What are predicates in java 8?

0 Answers  


Why collection is called framework in java?

0 Answers  


Which package is used for pattern matching with regular expressions?

0 Answers  


What are the two ways you can synchronize a block of code?

5 Answers   Ericsson,


what is meant by Byte code concept in Java?

0 Answers   Aspire,


Can a static method be overridden in java?

0 Answers  


1) There are 10 different threads in runnable state. Each having priority 1 to 10. How does the CPU schedules or executes these threads?

2 Answers   RBS,


Why is it called buffering?

0 Answers  


How does arraylist work in java?

0 Answers  


Can we sort hashmap in java?

0 Answers  


Categories