This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what happens when v compile...

 
ganapathy gopinathan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
ganapathy gopinathan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
ganapathy gopinathan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will the compiler replace that particular 'import' statement with any other reference, which uniqly identify that particular class on interpretation?
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u for ur info!!!


gopinathan
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ganapathy gopinathan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic