• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Local variables only available inside their method

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you help me on this? I need to use the Stack1 and Temp1 on different methods



 
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don’t use Stack: use this instead.
 
Meee De Lune
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I AM MIGHTY! Especially when I hold this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic