Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parameter.....

 
mark smith
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a JavaRanch FAQ page that addresses this very issue.
 
mark smith
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to downcast the input parameter which is type "Object" to FirstApp type first.
 
mark smith
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic