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

Can we override data members in java?

1085


What is the main purpose of serialization in java?

968


What is use of static in java?

1087


What are the types of arrays in java?

1024


These static constructors are correct ? class A { statc intA() { } static A(int x,int y) { } static A(int x) { } }

1028


What is the size of integer?

1045


Is it possible to use string in the switch case?

992


What is the difference between compiler and jvm?

979


Can we create constructor in abstract class ?

987


What are the two environment variables that must be set in order to run any java programs?

905


Can we create object of static class?

970


What is the flag in java?

991


Under what conditions is an object’s finalize() method invoked by the garbage collector?

977


What is the use of 'super' keyword inside a constructor?

907


When can you say a graph to be a tree?

1027