aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Program Running successfully even there is no class defintion in the same class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Program Running successfully even there is no class defintion in the same class" Watch "Program Running successfully even there is no class defintion in the same class" New topic
Author

Program Running successfully even there is no class defintion in the same class

Sekhar Choudary
Ranch Hand

Joined: May 03, 2008
Posts: 57
Hi,
I have to two java programs in the same directory.
say JavaFile1.java and
JavaFile2.java

And in JavaFile1.java,
has two classes A and B

Now in JavaFile2.java ,
has two class definitions for A1 and B1

but unfortunately in this JavaFile2.java,instead of creating object for A1 ,i have created object for A as follows. Actually there should be statement
A1 ob=new A1();
but i have created it like this
A ob=new A();
Now , my doubt is when i run the second program i.e., JavaFile2.java,
Even though there is no class named A inside this, the compiler is not pointing out.
It's simply creating an object of class and is invoking its A's no-arg constructor into JavaFile2.java
NOTE::No import statements used

Can anyone let me know the reason.
Thanks.
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
classes in a package are visible for all classes in the same package without needing to import. :-)
[ July 30, 2008: Message edited by: Raphael Rabadan ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Sekhar,

besides the reason Rafael described, there is another possibility: If your class doesn't have a package and you use the command

javac JavaFile1

in your directory, than the class file is put into that directory, too. If you then use

javac JavaFile2.java
java JavaFile2

the current directory is taken as classpath (if you didn't define it as environment variable). Thus compiler and runtime will find A.class.


SCJP 5 (98%) - SCBCD 5 (98%)
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Originally posted by Ralph Jaus:
Hi Sekhar,

besides the reason Rafael described, there is another possibility: If your class doesn't have a package and you use the command

javac JavaFile1

in your directory, than the class file is put into that directory, too. If you then use

javac JavaFile2.java
java JavaFile2

the current directory is taken as classpath (if you didn't define it as environment variable). Thus compiler and runtime will find A.class.


Ralph, getting your example, so, I dont need to give the classpath to the JavaFile1 because its in the same package of the JavaFile2, is it?
When i dont need to give the classpath to the classes im running with the command java(and i dont have any classpath in environment).??

I think i'm missing something here, because i thought we always needed to specify the classpath.
[ July 30, 2008: Message edited by: Raphael Rabadan ]
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Raphael,

if you don't specify a classpath (neither in java/javac command nor in environment), java/javac just take the current directory as classpath. For example

javac JavaFile2.java

is equivalent to

javac -classpath . JavaFile2.java
[ July 30, 2008: Message edited by: Ralph Jaus ]
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Originally posted by Ralph Jaus:
Hi Raphael,

if you don't specify a classpath (neither in java/javac command nor in environment), java/javac just take the current directory as classpath. For example

javac JavaFile2.java

is equivalent to

javac -classpath . JavaFile2.java

[ July 30, 2008: Message edited by: Ralph Jaus ]


Thanks Ralph :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program Running successfully even there is no class defintion in the same class