• Post Reply Bookmark Topic Watch Topic
  • New Topic

how can my function read a previously assigned variable?  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

making a simple online shop that sells cows (I know...)
its menu driven
but for some reason i cant get my function to red a previously created variable
is it the way i have my function setup?



yes the if statements are incomplete,,just trying to get one option going first...thanks
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right at the bottom cows is in red and is not recognized by my function
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite clear. What's working? And, what's not working? What input are you giving it? What results do you expect?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:right at the bottom cows is in red and is not recognized by my function

'cows' is a variable local to the main() method, it is not accessible in your other method. Either pass cows to your other method or make cows an instance variable (a.k.a. field), instead of a local variable.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your add_cow() method is not static so if you want to call it you'll need an instance of your KeyChainsForReal class for it to operate on.
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to make the cows variable visible to the add_cow() and other methods that will use it.  There are two ways to do this:

1. Define cows outside of both methods and make the variable and both methods static:


2. Make cows and instance variable, move the guts of main() to a new method and create an instance of your class to do the work:


While these will both work, option 2 is better.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a ton guys,

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is method 1 less preferred from a security point of view? should you never do this?
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so i have it working along thee lines of number 2

but i just looked a your example and tailored my code accordingly

but i dont really understand what i did...well this is what i think i done

KeyChainsForReal kcfr = new KeyChainsForReal();
        kcfr.run(args);                                                          ----------------dont really know what this line does...
    }
        public void run (String[]args) {

created a new object called kcfr from the keychainsforreal class
kcfr.run is now the main method? ie where the program is running from?

i really don know a lot about classes and public static etc

is there a good tutorial you know that could explain all of this

thanks again for the help


 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This creates an object kcfr which is the class we're in.

This calls the method run and passes args as an argument.

If this is unclear to you, you probably should start with this tutorial.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would this be a typical way to start a program? would you always create a object from your class to run as main...is this the preferred way?

 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's fairly standard, yes. There's a rule of thumb that you shouldn't just throw everything into one big method, you should break up big methods into smaller methods, each of which has a well-defined purpose. For example that code which displays the menu of options to the user? You could break that out into a method called, say "displayMenu". And so on. Once your "one big method" gets beyond 100 lines of code it starts getting too hard to maintain.

As for the idea of using an object... After you've been programming for a while you'll start to write reusable pieces of code. After you've copied and pasted the same code into three different programs you'll find it gets pretty old, so you put that code into a separate class and use an object of that class to call it as required. You should get to that later on in your course so I'm not going to go into detail now.
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:would this be a typical way to start a program? would you always create a object from your class to run as main...is this the preferred way?


For all but the simplest, smallest, shortest-lived programs, I always create an object and execute a run() or similar method.  This is especially important if the "guts" of your program will ever be reused as part of a web site or in any other context that might end up being multi-threaded.  It's better to develop the habit of creating a object and running a method in that and then  breaking that habit when appropriate than it is to develop the habit of always using static methods and trying to remember when to break that.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find it sort of unfortunate, I see all these beginner programmers asking questions about this sort of code here. But in the real world Java programmers almost never write code which is anything like that. I just have to keep telling myself "These people aren't learning Java, they are learning programming."
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry paul,

not really with you...what do you mean?

do you mean that real world java programmers hardly do any of this stuff? what are they doing..can you please explain a little more
interesting
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write ==  true or ==  false. Both are poor style and error‑prone in case you write = by mistake.
It is not if (somethingBoolean == true) ... but if (somethingBoolean) ...
The same applies to == false
Nor if (somethingBoolean == false) ... but if (!somethingBoolean) ...
The same applies to Boolean expressions in any context.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!