aspose file tools*
The moose likes Beginning Java and the fly likes parameter..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parameter....." Watch "parameter....." New topic
Author

parameter.....

mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
hi

i use a variable in a class
i pass it to a method to another class, the method change the value


first class


second class


why FirstApp display 15 and not 30?

thanks
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Well the parameter you send to the method is a primitive, and the value of the actual parameter is unaffected by what you do in the method.
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
primitive is passing by value and object by reference...

that work with this code


with this code, i need to know the class name for setAgeB.....
if i create a third class with a main i can use SecondApp.... i tried to change FirstApp type in the setAgeB method by Object but i get

SecondApp.java:7: cannot find symbol
symbol : method getAge()
location: class java.lang.Object
m_ageB=tmp.getAge() * 2 ;
^
SecondApp.java:8: cannot find symbol
symbol : method setAge(int)
location: class java.lang.Object
tmp.setAge(m_ageB);


with



any idea?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by mark smith:
primitive is passing by value and object by reference...


All parameter passing in Java is by value.

Also notice that since SecondApp does not extend FirstApp, it does not have the methods of FirstApp.
[ May 15, 2006: Message edited by: Keith Lynn ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

There is a JavaRanch FAQ page that addresses this very issue.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
ok but that don't resolve my problem of my second post...



setAgeB can only be used by FirstApp object.....
what can i do if i want another objet type use this method (setAgeB)?

thanks
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Do you mean you want to send another object to the method. If the parameter type is FirstApp, then you can only send an object whose type is FirstApp or a subclass of FirstApp.
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
Originally posted by Keith Lynn:
Do you mean you want to send another object to the method. If the parameter type is FirstApp, then you can only send an object whose type is FirstApp or a subclass of FirstApp.


ya another object to the method.....

i can't do something like that
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
you need to downcast the input parameter which is type "Object" to FirstApp type first.
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
Originally posted by wise owen:
you need to downcast the input parameter which is type "Object" to FirstApp type first.




i get


SecondApp.java:7: cannot find symbol
symbol : method getAge()
location: class java.lang.Object
m_ageB=tmp.getAge() * 2 ;
^
SecondApp.java:8: cannot find symbol
symbol : method setAge(int)
location: class java.lang.Object
tmp.setAge(m_ageB);
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

Mark, the reason you get that error is because you did not do what wise owen told you:
Originally posted by wise owen:
you need to downcast the input parameter which is type "Object" to FirstApp type first.


You have an Object and you are trying to call the method getAge() on it. This method does not exist in class Object. You need to cast the Object to FirstApp:



Another solution is to change the type of the parameter of the method setAgeB to FirstApp instead of Object:



Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

You have an Object and you are trying to call the method getAge() on it. This method does not exist in class Object. You need to cast the Object to FirstApp:


I am not convinced that this is what Mark wants... From his question, I got the impression that he wants to use the methods for any object -- not just firstApp objects that is referenced as Object objects.

The cast will allow it to compile, but will throw an exception at runtime, if he passes any object that is not an instanceof FirstApp object.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parameter.....