Java constructors. Assigning a value to a variable

Is there a difference between assigning values ​​to variables outside of any method and assigning these values ​​inside the constructor?

Looking at the Oracle Java tutorial, they have:

public class Bicycle { int cadence = 0; int speed = 0; int gear = 1; void changeCadence(int newValue) { cadence = newValue; } 

it is nothing to say / why they simply did not say:

 Bicycle(){ int cadence = 0; } 
+4
source share
7 answers

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

+6
source

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

+3
source

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

But if you mean:

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

vs

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

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

+3
source

When you instantiate an object, global variables will be initialized. You can (but not necessarily) modify some of them in the constructor.

I think you mean

 Bicycle() { cadence = 0; } 
+2
source

In the constructor: this will be a local variable

Java contains the following types of variables:

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

Class variables (static fields): They all belong to the class, and not one of the objects can require its sole owner. Variables defined by the static keyword are shared by all objects. Here, the Objects do not retain individual significance, but they are forced to share it with each other. These variables are declared as "static fields" using the static keyword. The same set of values ​​is always used for different objects of the same class. Thus, these variables are similar to global variables that are the same for all objects in the class. These variables take default values ​​if not initialized.

Local variables: Variables defined in a method or block of code are called local variables. Access to these variables is possible only within the framework of a method or code block. These variables do not accept default values ​​if they are not initialized. These values ​​must be initialized before using them.

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

+1
source

The constructor is similar to any other method. Variables declared inside it are accessible only inside it, and they will be destroyed when you exit the scope.

+1
source

The difference in this case is that you not only assign a variable in the constructor (in the second case). You also declare this. Thus, the variable is local to the constructor and not visible from the outside.

+1
source

All Articles