Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

How java uses the string and stringbuffer classes?

1061


What is intern method in java?

1071


Can we compare two strings in java?

1015


How is hashcode calculated in java?

1020


Can you call a method in a method?

1081


Difference between keyword and identifier.

1021


What are the different data types in java?

996


What is the difference between jdk and jre?

1051


Can a class extend more than one class?

1116


Give few difference between constructor and method?

957


What is the default value of the local variables?

1012


List out benefits of object oriented programming language?

884


What is a method type?

997


How is treeset implemented in java?

1021


What are examples of modifiers?

1114