This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pass reference variable in method

 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written this code and am getting 4 compilation errors. But am still not able to correct it. Please help



and the output on command prompt was:
C:\Users\Anjali\Desktop\PracJava\Ref>javac MyObject.java
MyObject.java:9: cannot find symbol
symbol : constructor MyObject(java.lang.String)
location: class MyObject
MyObject obj1=new MyObject("Hello");
^
MyObject.java:10: non-static method myTest(MyObject) cannot be referenced from a
static context
myTest(obj1);
^
MyObject.java:12: cannot find symbol
symbol : constructor MyObject(java.lang.String)
location: class MyObject
MyObject obj2=new MyObject("World");
^
MyObject.java:13: non-static method myTest(MyObject) cannot be referenced from a
static context
myTest(obj2);
^
4 errors
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can compile the code you provided without any errors.
 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked it again. In a new cmd window again compilation error with the same 4 errors
What is the problem? other programs are executing properly
 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you aren't editing a copy of the file you're trying to compile?

Maybe you accidentally renamed the file or saved it to another folder.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
agreed. I did a straight cut-n-paste and it compiled and ran:

C:\slop>javac MyObject.java

C:\slop>java MyObject
in constructor Hello
in myTest MyObject@3e25a5
in constructor World
in myTest MyObject@19821f

C:\slop>
Try saving it under a different file name like MyObject2.java (you'll have to change your class name in the file as well), and make sure it's saving it where you think it is.
 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it because am running in a cmd. Is it the reason why it's giving errors because it interprets line by line?
 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, cmd should be fine. Have you already checked that the filenames and -paths in the command prompt and in your editor are the same?
 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I now renamed all the MyObject to MyObject2 and it worked perfectly fine. But why did this happen? Just asking so that i know where i went wrong if this happens again
 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a guess, but when you were editing the file, you might have used Save As... instead of Save when you tried to save the file, and accidentally renamed the file. When you continue editing the file, the changes won't be visible to the compiler because you're telling it to compile the old copy of your file.

It's likely just a fluke. I wouldn't worry about it, I don't think it will happen again any time soon.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:This is just a guess, but when you were editing the file, you might have used Save As... instead of Save when you tried to save the file, and accidentally renamed the file.
Or saved it to a different directory.

If it happens again, look at the time stamp on the file in your cmd window when you do a dir. Then edit and save the file again (you don't have to change much - just put in then delete a space so the file has "changed" then save it). Then do another dir and see if the timestamp changed. If not, they you're saving it somewhere else...
 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for the advice.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic