aspose file tools*
The moose likes Beginning Java and the fly likes class instantiation 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 "class instantiation" Watch "class instantiation" New topic
Author

class instantiation

Anshuman Shukla
Greenhorn

Joined: Oct 11, 2012
Posts: 4
I have created one file TestAB.java in a directory, this file has two classes TestA and TestAB. class TestAB has the main method where I create the instance of TestA. TestAB.java compiles successfully and runs as expected.
Now, in another directory I have saved class TestA and class Test AB in two different files TestA.java and Test AB.java, TestA.java compiles successfully but when I compile TestAB.java I get the following error:

TestAB.java:5: error: cannot find symbol
TestA ta = new TestA();
^
symbol: class TestA
location: class TestAB
TestAB.java:5: error: cannot find symbol
TestA ta = new TestA();
^
symbol: class TestA
location: class TestAB
2 errors

Why is the compiler giving these errors? I thank you in advance for your help with this.

// Code
/* TestAB.java */
In a different folder
/* TestA.java *//* TestAB.java */
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Anshuman Shukla wrote:I have created...

Anshuman.

I've added code tags to your post. See how much better it is to read?
Please read the UseCodeTags (←click) page thoroughly.

You can also see what I did by clicking on the 'Edit' icon.

Thanks.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Anshuman Shukla
Greenhorn

Joined: Oct 11, 2012
Posts: 4
Hi Winston,
My post has become more readable after applying the tags. How were you able to edit my post?
Anshuman.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Anshuman Shukla wrote:My post has become more readable after applying the tags. How were you able to edit my post?

Ah, tho' but a lowly Bartender, I have certain mystical God-like powers...

Hopefully, I won't need to next time.

Winston
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to CodeRanch Anshuman Shukla!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

How are you compiling your code; on the command line, using javac?

You probably have the CLASSPATH environment variable set. Unset it, so that Java takes the current directory "." as the classpath. Or specify the classpath yourself when you compile your code:

javac -cp . TestAB.java

Then run it:

java -cp . TestAB

Some people here on the forums, for example Winston and myself, are "bartenders" (you can see that under our name on the left side). Bartenders are moderators for the forums: we keep an eye on what happens on the forums and we have the ability to edit other people's posts.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Anshuman Shukla
Greenhorn

Joined: Oct 11, 2012
Posts: 4
Hi Jesper,

I was half expecting the reply that I got from Winston, thanks for clarifying that 'Bartenders' are moderators. To answer your question, on the command prompt I type javac filename.java to compile my program and java -cp . filename to run it. I tried your recommendation of typing javac -cp . filename.java and my program compiled without errors and it runs as expected.
Now, I tried to insert an additional statement, package Example; in both the files TestAB.java and TestA.java, and tried compiling with javac -cp . Test AB.java, I am getting the same errors

TestAB.java:7: error: cannot find symbol
TestA ta = new TestA();
^
symbol: class TestA
location: class TestAB
TestAB.java:7: error: cannot find symbol
TestA ta = new TestA();
^
symbol: class TestA
location: class TestAB
2 errors

while TestA.java compiles with no errors. What could be the problem?

Also, I just checked that the CLASSPATH environment variable it is set to %JAVA_HOME%|jre|lib .

Thanks,
Anshuman.
Anshuman Shukla
Greenhorn

Joined: Oct 11, 2012
Posts: 4
Thanks Seetharaman Venkatasamy!
 
jQuery in Action, 2nd edition
 
subject: class instantiation
 
Similar Threads
Extended class, compilation error
Question on Thread and Runnable
ArrayList:
Help with compareTo()
HeadFirst Java, Ch3 Pool Puzzle