• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Referenec passing

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ Jess added UBB [code] tags to preserve whitespace ]
[ February 01, 2003: Message edited by: Jessica Sant ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


// could you please explain to me the difference between these two classes with regards to member variable str

First of all, I think it is a bad idea to assign an int to a variable named str. I suggest that you rename it to "number".
Second, str is not a member variable at all. It is a class variable and a local variable, but never a member variable. Because they are class variables, you do not need to instantiate the class in order to print them.

Thirdly, I'm not quite sure I understand what your question about these two class variables is. One is a String and the other is an int. Can you be a little more specific?
[ February 02, 2003: Message edited by: Marilyn de Queiroz ]
 
tony kanvas
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn de Queiroz
Second str is not a member variable at all. It is a class variable and a local variable, but never a member variable. Because they are class variables, you do not need to instantiate the class in order to print them.
First I was playing in code so I didn�t take care about constants
Second That what I want to know:
when we call the String or StringBuffer (Object)it will call class var
And if we call any number then we use local var is this true
Thanks for your time
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First I was playing in code so I didn�t take care about constants

They are not constants, they are class variables.
when we call the String or StringBuffer (Object)it will call class var
And if we call any number then we use local var is this true


Ummm, not exactly.


String constant "Outside variable" is assigned to String str class variable in line 3.

String constant "Inside variable" is assigned to String str local variable in line 10.

In line 11, you send the contents of local variable str (currently set to "Inside variable" ) to method modify().

Method modify assigns "a" to class variable str in line 6. So it no longer is "Outside variable".

Line 12, System.out.println( ton.str ); prints the class variable because you told it to. If you just print str rather than ton.str or i.str, you will get the contents of the local variable str.



int constant 22 is assigned to int str class variable in line 3.

int constant 21 is assigned to int str local variable in line 12.

In line 13, you send the contents of local variable str (currently set to 21 ) to method modify().

Method modify assigns the local int variable that you passed (21) to class variable str in line 6. Now local variable str and class variable str are the same int value.

Then you add 1 twice to the class variable str.

Now when you return to main and print, you again request the class variable str to be printed rather than requesting the local variable str to be printed. (See above)
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps it would be clearer if you changed the variable names. What you have could be written this way:
 
tony kanvas
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your help
best wishes
tony (sweden)
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO the members of a class are the fields, methods and nested types both declared and inherited in that class.
Constructors, static initializers and instance initializers are not members and they are not inherited.
 
reply
    Bookmark Topic Watch Topic
  • New Topic