File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes pass reference variable in method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pass reference variable in method" Watch "pass reference variable in method" New topic
Author

pass reference variable in method

Anjali Vaidya
Ranch Hand

Joined: Jan 25, 2011
Posts: 40

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

Joined: Oct 27, 2005
Posts: 19653
    
  18

I can compile the code you provided without any errors.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anjali Vaidya
Ranch Hand

Joined: Jan 25, 2011
Posts: 40

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

Joined: Sep 20, 2010
Posts: 3595
    
  14

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

Joined: Oct 02, 2003
Posts: 11155
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Anjali Vaidya
Ranch Hand

Joined: Jan 25, 2011
Posts: 40

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

Joined: Sep 20, 2010
Posts: 3595
    
  14

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

Joined: Jan 25, 2011
Posts: 40

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

Joined: Sep 20, 2010
Posts: 3595
    
  14

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

Joined: Oct 02, 2003
Posts: 11155
    
  16

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

Joined: Jan 25, 2011
Posts: 40

thanks a lot for the advice.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass reference variable in method
 
Similar Threads
Error in Java
constructor to add to an array list
Hashcode Problem
Output of the Program
Cannot resolve symbol