Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sekhar Choudary
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ralph Jaus
Ranch Hand
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic