5

Is there a difference between assigning values to variables outside of any method, and assigning these values within a constructor?

Looking at Oracle's Java tutorial, they have:

public class Bicycle {

   int cadence = 0;
   int speed = 0;
   int gear = 1;

   void changeCadence(int newValue) {
      cadence = newValue;
   }

is this any different from saying/why didn't they just say:

Bicycle(){
    int cadence = 0;
 } 
2
  • Did you mean class global varoable cadence in constructor? Commented Mar 25, 2013 at 22:20
  • I meant: what's the difference when you make a new Bicycle from public class Bicycle with and without a constructor? If it is Bicycle created without a constructor, are ints cadence, speed, gear, still passed to it? Commented Mar 25, 2013 at 22:38

8 Answers 8

6

If you declare the variable in your constructor, it will be local to the constructor and not visible anywhere else in your class.

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

Comments

3

If you declare a variable inside a constructor, this variable can only be accessed inside this constructor. But, you can create a variable on your class, and access it on your constructor or method.

Comments

3

Well if you define a variable in a constructor, it is local to the constructor. But a variable in the class is part of class' state.

But if you mean:

class A {
    int i = 1;
    ...
}

vs

class B {
    int i;

    B() {
        i = 1;
    }
    ...
}

the difference is: in the first case, the default value of i is 1, in all instances, but in second case the default value of i is 1 if the shown constructor is called, maybe in other constructors other default value be something else (or 0 if nothing is assigned to i).

1 Comment

+1 for clarifying that variables initialization can be done outside constructor. In c++, this is not allowed.
2

When you create object instance, global variables will be initialized. You can (but not have to) change some of them in constructor.

I think you mean

Bicycle() { 
   cadence = 0; 
} 

3 Comments

He is talking about alternative implementation. This is another code.
Dont think so - i think he's asking about init class variables from constructor.
just completing the code: the cadence variable must be defined outside (before) the constructor. If not, this won't compile
1

In the constructor : it will be Local variable

Java contains the following types of variables:

Instance Variables (Non-static fields): In object oriented programming, objects store their individual states in the "non-static fields" that is declared without the static keyword. Each object of the class has its own set of values for these non-static variables so we can say that these are related to objects (instances of the class).Hence these variables are also known as instance variables. These variables take default values if not initialized.

Class Variables (Static fields): These are collectively related to a class and none of the object can claim them its sole-proprietor . The variables defined with static keyword are shared by all objects. Here Objects do not store an individual value but they are forced to share it among themselves. These variables are declared as "static fields" using the static keyword. Always the same set of values is shared among different objects of the same class. So these variables are like global variables which are same for all objects of the class. These variables take default values if not initialized.

Local Variables: The variables defined in a method or block of code is called local variables. These variables can be accessed within a method or block of code only. These variables don't take default values if not initialized. These values are required to be initialized before using them.

Parameters: Parameters or arguments are variables used in method declarations.

Comments

1

The constructor is like any other method. Variables that are declared inside it are available only within it, and they'll get destroyed when you go out of scope.

Comments

1

The difference in this case is that you are not only assigning the variable in the constructor (in the second case). You are also declaring it there. Thus, the variable is local to the constructor and not visible from outside.

Comments

0

I think your question is: If we can give a default value to the variable(of a class) directly, why to give it inside a constructor instead. If that's the question, then I think its because the values in the variable would be available even when the objects are not initialized which is not a healthy practice and does not make much sense. Instead, giving values inside a constructor would not only be hidden but would also obey oops rules for java.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.