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...

Why only one Class is public in one file? Explain in
details. Thanks in Advance.

Answer Posted / slvganesh.java@gmail.com

A file may have more than one class. Each class may or may
not have main method. But only one main () for a class.

Here's some example: Just Copy it and Run..

/**
* Save this file as Class1.java, bcoz i'm using a class
* Class1 which is public.
*/

public class Class1 { // Note this line

/* This Class is public. So your file name should be the
same name.
*/

public static void main (String arg[]) {
System.out.println (" This is from Class1.. ");
} // Main
} // Class1

class Class2 { // Note this line
/* This class is not public. so it doesn't matter
your file name.
*/

public static void main (String arg[]) {
System.out.println (" This from Class2..");
}
}

class Class3 {
/* This class Calls the main() in Class1.
*/

static public void main (String arg[]) {
System.out.println (" This is from Class3..");

// Creating obj to Class1
Class1 c1 = new Class1 ();
c1.main (arg); //Main must have one arg as String[] type
}
}


Compile : javac Class1.java

After your compiled your can see three class files,
1. Class1.class
2. Class2.class
3. Class3.class

This is bcoz, java compiler converts the classes into files,
which has the bytecode instructions for that class.

Now you can execute the classes as you want.

Run : java Class1 (or)
java Class2 (or)
java Class3

After executing Class3 you may see the
Output :
This is from Class3..
This is from Class1..

Please understand, once you compiled your .java file, the
classes on that file will converted into .class (bytecodes).

Java interpreter looks only the .class.
In other words, java interpreter understand only the bytecodes.

Thanx for reading this..
Ganesh Slv.

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How many bytes is a string java?

978


What is high level language in computer?

984


how would you implement a thread pool? : Java thread

854


What is a nonetype?

1061


What is string subsequence method?

1084


How can we make a class virtual?

1072


What are the actions that can occur when a thread enters blocked state?

1020


What is currentthread()?

941


List the three steps for creating an object for a class?

919


What are the procedures?

1012


What is the use of a copy constructor?

964


Explain how can you debug the Java code?

1016


What is the right data type to represent a price in java?

919


Why set do not allow duplicates in java?

1024


What are the methods of object class ?

1016