• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

parameter.....

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a JavaRanch FAQ page that addresses this very issue.
 
mark smith
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

 
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
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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic