Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local variables only available inside their method

 
Meee De Lune
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you help me on this? I need to use the Stack1 and Temp1 on different methods



 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Please don’t post unrelated questions on an old thread. I shall create a new thread.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have completely misunderstood the difference between local variables which are only “visible” inside their methods.
You should have fields which are private and therefore “visible” throughout the whole class. You should consider getXXX and setXXX methods to provide that information to other classes. You should initialise all your fields in the constructor.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don’t use Stack: use this instead.
 
Meee De Lune
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that my problem was related to the thread so i post my reply/problem there. I'm not that good in coding stuffs. I'm currently doing my project, and it requires me to use only stack. Can you give me a simple example how do getxxx and setxxx is used.
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Honestly, I have no idea with this Stack class yet.. ? But if you code like above, may be it will full fill your need... I don't know...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks all right to me now. I can’t find any errors now ( ). What is happening which you didn’t expect.
 
Meee De Lune
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i realize my errors, first I need to initialize my stack variables before the "create" method so i can use them in other methods such as main, and another thing is how i print variables. Question, how can we define the limit for a stack variable? Can i ask another question which is different to the thread topic. Is it possible to have two return values in a method? one is to terminate a program and the 2nd is to return a value?
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to have two return values in a method? one is to terminate a program and the 2nd is to return a value?


I think you haven't explained it clearly as you would like... ?
Do you mean same time.. or two return types ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meee De Lune wrote: . . . Is it possible to have two return values in a method?
No. You can however encapsulate several values in an object and return a reference to that object.
one is to terminate a program and the 2nd is to return a value?
A return value which terminates the program sounds like very poor design to me.You can have a flag which keeps the program running, however. When you turn that false, the while loop terminates.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic