wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes calling same named variables in same class but in different subgroup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling same named variables in same class but in different subgroup" Watch "calling same named variables in same class but in different subgroup" New topic
Author

calling same named variables in same class but in different subgroup

brent carter
Ranch Hand

Joined: Dec 15, 2011
Posts: 34
My program was messing up and combing two like-named variables even though they were in a different subgroup (what is it called something in a class but with its own public void abc(){ situation?) so I did this as a remedy. Is there an easier way or a command that does this instead of having to make different variables for everything? I.E. can i just have something like fighterstats.strength and magestats.strength similar to caling variables in classes?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11246
    
  16

What do you mean by 'subgroups'? That is not really a java term. There are member variables and local/method variables - that's it.

Tell us what you are actually trying to DO, rather than show us a hack that solves your problem and asking for a better way.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
brent carter
Ranch Hand

Joined: Dec 15, 2011
Posts: 34
part of the trouble I am having is that I know so little so it is difficult for me to articulate my questions. basically what I didn't know about and just found out researching was making a variable static vs non-static. Form my understanding when I make a variable for ex: "static int charpick" at the class level, once it gets defined in that class it stays that definition unless it is changed somewhere else. Unchanged, it retains its current value even when called from another class. Is this accurate?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

brent carter wrote:part of the trouble I am having is that I know so little so it is difficult for me to articulate my questions. basically what I didn't know about and just found out researching was making a variable static vs non-static. Form my understanding when I make a variable for ex: "static int charpick" at the class level, once it gets defined in that class it stays that definition unless it is changed somewhere else. Unchanged, it retains its current value even when called from another class. Is this accurate?


No.

Static means "associated with the class as a whole, not with any particular instance." Do you understand the difference between a class and an instance? For a static member variable, there is one copy, that is shared by the class as a whole and by all instances of the class. For a non-static member variable, there is a separate copy for each instance.

Both of those are member variables, which exist outside of any particular method or constructor. Distinct from member variables are local variables, which spring into existence when a method or constructor is invoked, or during its execution, and cease to exist once that method or c'tor has finished executing.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11246
    
  16

to further illustrate the difference between a class and an instance...

I can draw up a blueprint of a house. I can list various things about it:

bedroom1Color
bedroom2Color
kitchenColor

etc.

Now, I can use that same blueprint to build 20 houses, all are basically the same. However, each and every house I build has its own value for bedroom1Color. Changing the color of the bedroom in one house has no affect on the color of the other 19 bedrooms.

your .java file is the blueprint that tells Java how to build the objects. You can build as many copies as you want, and each class has its own copy of all the variables - unless you declare it as 'static'. A (poor) example of a static variable may be a counter of how many times you've created such an object. You only need (and only want) one place to store that, and all instances can access it.

What you probably want to do is read up on polymorphism and inheritance. your fighter and mage both have certain things in common, so you want to abastract them out into a superclass. Generally, it would look something like this:



Now, you can make either a fighter or a mage, but you can treat both as genericClass objects. You can make an arrray that holds genericClass references. The, when you loop through it, you can call 'setStats' on each, and the fighters will have fighter stats set, and mages will have mage stats set. a year from now, you can write a theif class, and stick it in your array, and it will also work when you call 'setStats', since it will also be a child of genericClass...
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
Unchanged, it retains its current value even when called from another class. Is this accurate?


When the jvm loads up it looks through the classpath for everything and anything marked static. When it finds something it loads up one instance of the class where that static thing -- a method, a constant, whatever -- is found. It puts that instance in a special roped-off area you don't have direct access to. It is to this instance that the compiler directs all references to Static Thing. (I don't know precisely how this works but that's the general idea.) So if you do this:


any reference in your code to BANANA_COLOR goes to that hidden instance of Monkey, an instance you have no other access to. And you don't have to, and should not, create an instance of Monkey at all (unless you also have non-static stuff on it), because the compiler treats references to Static Thing as being invoked on the class, not on any instance of the class. In other words, these lines are identical:


In sum, static stuff belongs to the class and is visible everywhere the class is visible. Statics are a good choice for constants or methods that don't rely on any value specific to that object. An example is the Math class. You never have to create one, you just invoke methods on it. It's just there, ready for use.

I'm pretty new to this myself so perhaps someone who knows better will step in here and correct my blunders!


Costs matter. Justice lies in processes not outcomes. Crime is caused by criminals.
brent carter
Ranch Hand

Joined: Dec 15, 2011
Posts: 34
Thanks guys very much. This is much clearer to me now. I see that my problem was I assumed the program would know which stats I wanted to call, without creating a separate class for fighter or mage. Now I see that it is impossible for the computer to know this without further specification. I also see how much easier it makes it to change the program later on. One last quick question on this thread. Is there every a problem of having too many classes? Is it good coding procedure to try and limit the class number to some degree or is it good to have many classes that do very specific things?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Thomas Kennedy wrote:
When the jvm loads up it looks through the classpath for everything and anything marked static


No, it doesn't. I determines what's static and what's not when classes are loaded (or, more properly, when they're initialized, but the two generally go hand-in-hand), which happens on-demand--that is, when the class is first referenced in executing cod.e


. When it finds something it loads up one instance of the class where that static thing -- a method, a constant, whatever -- is found.


No. It only creates instances when you tell it to, such as with the new operator or Class.newInstance(), etc.


It puts that instance in a special roped-off area you don't have direct access to.


No, there's no special roped-off area. Class-level members are just as accessible as instance-level members.


It is to this instance that the compiler directs all references to Static Thing.


No. Again, there's no special instance for statics.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

brent carter wrote:One last quick question on this thread. Is there every a problem of having too many classes? Is it good coding procedure to try and limit the class number to some degree or is it good to have many classes that do very specific things?


In theory, yes, there could be problems with too many classes.

1) If you have a bazillion itty bitty classes, it could get to be a nightmare for you the programmer to maintain.

2) Like everything in the the world, the JVM and the hardware it runs on are finite, so it's theoretically possible to have more classes than the JVM can manage or than the available hardware can store.

However, these are unlikely to be problems in the real world. For #1, the usual problem is the exact opposite: Beginners often feel that creating new classes is too much work, or are afraid that too many classes will be "inefficient", so they avoid creating separate classes when the design dictates that they should. For #2, well, let's just say you'll never get there and leave it at that.
brent carter
Ranch Hand

Joined: Dec 15, 2011
Posts: 34
I think the mistake I've been making this whole time is mistaking local instances with classes. For one, I still don't know what its called when something is only under the public void abc{ satement, for example:



What is that called? Is that a local instance? I've started making separate class extensions for things I would group into one class and it is coming together nicely.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

brent carter wrote:I think the mistake I've been making this whole time is mistaking local instances with classes.


There's no such thing as a "local instance". Here's some basic terminology:



brent carter
Ranch Hand

Joined: Dec 15, 2011
Posts: 34
thanks Jeff for the great post. This makes things a lot clearer. So just to make sure I understand, if i have something in a class like:

public void console(){
String name


the string name exists only in the method console. Once the method ends name goes away.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

brent carter wrote:thanks Jeff for the great post. This makes things a lot clearer. So just to make sure I understand, if i have something in a class like:

public void console(){
String name


the string name exists only in the method console. Once the method ends name goes away.


The variable name exists only while a given invocation of console() is executing, yes. However, the String object that the name variable points to may still hang around after the method is done, if there's another variable NOT local to console() that is also referring to it.

I hope that doesn't muddy the waters, but it's important to understand the distinction between an object and variables that refer to it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling same named variables in same class but in different subgroup