1

I've been learning Java for the past year in my free time, and so far, I have come across nothing on how to use variables correctly. By this, I mean, should I prefer to use instance variables, or just use variables in a single method.

For example, this is a piece of code I've written:

public class arsenalTroop { String[][] troopStats; String[][] weaponStats; String[][] armorStats; String[][] animalStats; String[] troops; String[] weapon; String[] armor; String[] animal; JLabel[] troopsArray; int troopTotal; int weaponTotal; int armorTotal; int lordTotal; int animalTotal; JFrame arsenalLordFrame = new JFrame(); JTextField name = new JTextField(); JLayeredPane lP = new JLayeredPane(); JLayeredPane fP = new JLayeredPane(); JLayeredPane ldP= new JLayeredPane(); JLabel siegeLabel = new JLabel(); JComboBox weaponCB; JComboBox armorCB; JComboBox animalCB; JLabel siegeText = new JLabel(); JButton addWep = new JButton(); JButton addName = new JButton(); JButton addArmor = new JButton(); JButton addAnimal = new JButton(); JButton ntr = new JButton(); JButton nwe = new JButton(); JButton nar = new JButton(); JButton nan = new JButton(); JButton newWeaponButtonArray[] = new JButton[6]; JButton newArmorButtonArray[] = new JButton[6]; JButton newAnimalButtonArray[] = new JButton[6]; Border whiteBorder = BorderFactory.createLineBorder(Color.white); Border greyBorder = BorderFactory.createLineBorder(Color.gray); boolean[] weaponFree = new boolean[6]; boolean[] armorFree = new boolean[6]; boolean[] animalFree = new boolean[6]; JButton done = new JButton(); public void arsenalTroopGui() { 

As you can see, all of the variables I'm going to use are instance variables, so what i'm asking is, should I do this, or should I try to limit most of my variables to be within each method that they'd be used in?

3
  • It depends on the scope you want those variables to have... Commented Mar 7, 2012 at 8:00
  • 1
    Ya, I understand that, but I was wondering if I should use variables like this. I learnt programing first as a structured from my dad, and then I moved to Java and am wondering what is correct to do. Commented Mar 7, 2012 at 8:05
  • 2
    was about to -1 for refusing to clean up the code formatting, but noticed that it's more a quirk of SO which can't handle tabs (or better said: mixture of tabs and spaces). The way out is to not use tabs for code indenting, IDEs typically have some global setting to convert them to spaces Commented Mar 7, 2012 at 11:54

4 Answers 4

5

Rule of thumb: variables should be declared in the smallest scope possible.

Sign up to request clarification or add additional context in comments.

Comments

4

You should use instance variables [fields] if the class has state, and it is important to it.

You should use local variables [or method variables] when it is specific for each run of the method.

Note that it is not only coding style issue, think what will happen if you declare all your variables as instance variables - and then run a method concurrently twice. It will cause the invokations of the methods to change the variables of each other, while this issue doesn't happen for local variables.

Comments

1

It's so normal and common that in GUI classes which are implemented by Swing have a lot of fields in a class-wide scope because of layout component declarations. No need to worry about that.

Just make sure that you have designed your classes to be atomic and independent as much as possible. You can achieve this by putting some thoughts when declaring your variable to decide whether it's needed in a narrower scope, say specific to a method (local variable) or class, package, etc wide.

Comments

1

Java has a Term Scope. We can mean scope as a life range of any object or variable. Some scopes are as follows- Class scope (Instance variable) Method scope(Method variable) etc...

As you should know each object or variable consume memory during its life time. so we need to limit the life time of object or variable(actually primitive data) as much as possible to optimize memory utilization.

So try to put variable to method as much as possible.

2 Comments

Java has a garbage collector for this
but garbage collector can not wipe out the object until the object is referencing by any identifier. We make object suitable for garbage collection by limit its scope..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.