• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

class instantiation

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 */
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Anshuman Shukla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston,
My post has become more readable after applying the tags. How were you able to edit my post?
Anshuman.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch Anshuman Shukla!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anshuman Shukla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seetharaman Venkatasamy!
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic