File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Local variables only available inside their method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Local variables only available inside their method" Watch "Local variables only available inside their method" New topic
Author

Local variables only available inside their method

Meee De Lune
Greenhorn

Joined: Feb 26, 2013
Posts: 3
Can you help me on this? I need to use the Stack1 and Temp1 on different methods



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Welcome to the Ranch
Please don’t post unrelated questions on an old thread. I shall create a new thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
Don’t use Stack: use this instead.
Meee De Lune
Greenhorn

Joined: Feb 26, 2013
Posts: 3
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.
Ranjith Suranga
Ranch Hand

Joined: Oct 28, 2012
Posts: 84

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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Feb 26, 2013
Posts: 3
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?
Ranjith Suranga
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local variables only available inside their method
 
Similar Threads
Stack problem
JQ+ Qs.....
The program is compiling and running successfully but the only problem is that the output is printed
nullpointerexception
getting output without creating object ?