Kai
Kai

Reputation: 291

Java Variables Conventions

I'm confused with variable declarations in Java code.

I read... don't try to use global variables declarations .

Don't use something like this:

package cls;
public class test { 
   private String var;

   public someMethod(){ ... } 
}

And use ?

package cls;
public class test { 

   public someMethod(){ 
       String var = null;
   } 
}

I don't know which is the correct way....

Upvotes: 0

Views: 2411

Answers (4)

Dave Newton
Dave Newton

Reputation: 160261

It totally depends on what you need.

Your first example, however, isn't a global variable--it's an instance variable.

Variables should have as small a scope as possible. This makes code easier to reason about.

Instance variables are central to what OOP is all about--classes (objects) exist to encapsulate object state, and the methods that operate on that state. For example, a Person class would likely have first and last name instance variables:

public class Person {
    private String firstName;
    private String lastName;
    // Plus getters and setters for each.
}

This allows instance methods to access the first and last name variables directly without having to pass them between all the methods that need them.

The second example is a local variable. It's visible only in the method it's declared in. A reference to it may be passed to other methods.

Upvotes: 2

AusCBloke
AusCBloke

Reputation: 18502

You can't have truly "global" variables in Java the same way as you can in a language such as C. Java forces you to structure your program in an object oriented way.

In your example above, if var is required throughout a whole test object and is important to have stored, then you would use the first example. If var is only required in someMethod and it's not important for a test object to store it then use the second example.

Take note that even with the first example, var is encapsulated within a test object, so it's not really "global" at all, apart from maybe slightly to the member function of test (which is the whole point of instance/member variables).

The closest thing in Java to "global" data is something like:

public class GlobalVars {
   public static int globalInt;
}

And you could access globalInt throughout your code as GlobalVars.globalInt, without creating an instance of GlobalVars.

Upvotes: 1

Adam Zalcman
Adam Zalcman

Reputation: 27233

Java doesn't have global variables. The first variable is class level and maintains the state of class instances and hence exists as long as an instance of the class while the second is a method's local variable that exists only during method's execution. You can use the first variable to store state information that spans multiple method calls. The second variable disappears as soon as the control leaves the method. Also, every time you call the method another variable, accessible by the same local name is created on the stack.

Upvotes: 1

Todd
Todd

Reputation: 1882

Both are correct. Neither of those are global variables. The first one is a class field. It's unique to each instance of the class that you make. Class fields (ie. variable) stay with the instance of the class until the class itself is deleted.

The second one is a method scope variable. It's only there for temporary purposes to perform the calculations needed for the method to work, and once the code in the method is done, the variable goes away.

You use each for different purposes. For example, if you're writing a Car class, you'd probably have a class field for SteeringWheel and Brake. But if you had a method to calculate the average miles per gallon, you might create a method scoped variable to help perform the calculation.

Upvotes: 1

Related Questions