aspose file tools*
The moose likes Beginning Java and the fly likes what happens when v compile... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what happens when v compile..." Watch "what happens when v compile..." New topic
Author

what happens when v compile...

ganapathy gopinathan
Ranch Hand

Joined: Aug 10, 2004
Posts: 30
Hi all,

Anyone plz help me to understand, 'what happens when v compile a java class'. consider the example



In the above program, i hav imported to packages. i did not use any of the classes in those package.

MY QUESTION IS:
what the compiled class of 'a' will contain?
will it contain all the classes of those packages? or
will only contain the class 'a' itself?

Thank u
gopinathan
[ September 29, 2004: Message edited by: ganapathy gopinathan ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

"import" is just an instruction to the compiler. The compiler needs to find other classes so it knows whether your usage of the other classes is legal, but the other classes aren't copied into your .class file. You could import a thousand other packages, and it wouldn't change the size of your .class file by a single byte.


[Jess in Action][AskingGoodQuestions]
ganapathy gopinathan
Ranch Hand

Joined: Aug 10, 2004
Posts: 30
Hi,

If i use one class from that imported package then wat wud b the case.in that case wat ll happen? that particular class is included or not?

Thank u

gopinathan
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

No. No classes are ever included in any other class. Every class is in its own separate file. "import" just tells the compiler where to find other classes so that the compiler can make sure you're passing the right arguments when you call the other class's methods, and so on.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
If you use the class, either by subclassing or by instantiation, then the classloader will bring it into your object code from a class file and thus make your runtime code larger.

By the way, it would be easier to understand your posts if you don't use so many abbreviations. They make reading the post sortof like figuring out a whole bunch of personalized license plates at one time.

Thanks!

[ September 29, 2004: Message edited by: Jeff Bosch ]
[ September 29, 2004: Message edited by: Jeff Bosch ]

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
ganapathy gopinathan
Ranch Hand

Joined: Aug 10, 2004
Posts: 30
will the compiler replace that particular 'import' statement with any other reference, which uniqly identify that particular class on interpretation?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Probably. Classes are assigned id's. The Java Language Specification on Sun's website has that information, though it's hardly a beginner topic. Just curious -- what are you trying to figure out here? What will you use the information for?

Thanks.
ganapathy gopinathan
Ranch Hand

Joined: Aug 10, 2004
Posts: 30
Thank u for ur info!!!


gopinathan
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by ganapathy gopinathan:
will the compiler replace that particular 'import' statement with any other reference, which uniqly identify that particular class on interpretation?


Yes - the byte code *always* contains fully qualified class names. You can use them in your source code, too.

So you can write

java.io.File file = new java.io.File(".");

and don't use any imports at all - but that would become quite tedious. That's what imports are for: to allow you to not write the fully qualified again and again:

import java.io.File;
...
File file = new File(".");

That compiles to *exactly* the same byte code, it is just a shorthand.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Basically, import statements let you abbreviate class names, leaving off the package name. The compiled bytecode will have the full class name, including its package name.

This means your compiled program will include a reference to the class in the limited sense that the compiler uses the CLASSPATH to figure out which package contains the class you want from among your own package and the packages referred to in import statements. It then concatenates the proper package name with the class name you used in your source program and uses the full name in the bytecode.

However, even though the compiler locates a .class file for each class you want and checks that you used that class correctly, it doesn't include the actual location of that .class file in the bytecode, just the fully qualified class name. This means that at execution time your CLASSPATH can be different than at compile time (often on a different machine) and a .class file used at execution time may be different than one you used at compile time. However, if the members of the class found at execution time don't match your source program as to member name and method signature, the jvm wil raise an exception.

This is one reason that many instructors recommend that you start learning Java using the command line interface so you learn to get the CLASSPATHs and directory structure correct and can debug them later. IDE's manage this for you but then you don't learn.


Mike Gershman
SCJP 1.4, SCWCD in process
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Originally posted by ganapathy gopinathan:
Hi all,







[ September 29, 2004: Message edited by: ganapathy gopinathan ]



import statement should define in each class/interface use in program only (It's good to coding).

Example


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
ganapathy gopinathan
Ranch Hand

Joined: Aug 10, 2004
Posts: 30
Thanks to u all for giving the valuable response!!!

once again Thank u,
gopinathan

[ September 30, 2004: Message edited by: ganapathy gopinathan ]
[ September 30, 2004: Message edited by: ganapathy gopinathan ]
 
Consider Paul's rocket mass heater.
 
subject: what happens when v compile...
 
Similar Threads
package problem
[VisualAge][Weblogic] problems using weblogic with VAJ last version (4.0)
Generics are going to be the death of me.
Marcus Exam No2 #57
Object "Savvy"