Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Constructors

I am trying to learn how to specify class constructors in Java. I am starting to understand that they specify the types of instance variables of objects made from that class. They also can be used to set the instance variable initial values. The follow example is from the Java tutorial on Sun's website:

public Bicycle(int startCadence, int startSpeed, int startGear) {     gear = startGear;     cadence = startCadence;     speed = startSpeed; } 

Where in your classes source code should you put the constructor(s)?

Are these arguments the names of the variables?: (int startCadence, int startSpeed, int startGear) or are gear, cadence and speed the names of the variables?

What is the difference between (int startCadence, int startSpeed, int startGear) and gear, cadence and speed?

In the unlikely event that my instructor or any administrator from Salt Lake Community College ever comes across this question, let me make my intentions clear. This question is posted in the greatest spirit of academic honesty. I ask this question to seek general advice and help in understanding the proper way to use the Java programming language. I in no way use the work of others and represent it as my own work. I use the answers provided here as a general aid in my understanding. I do all my own work and do not copy work provided by people answering my question.

like image 496
Patrick Cassell Avatar asked Feb 23 '09 21:02

Patrick Cassell


People also ask

What are constructors in Java?

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. In Java, a constructor is a block of codes similar to the method.

What are the 3 types of constructor?

Constructors in C++ are the member functions that get invoked when an object of a class is created. There are mainly 3 types of constructors in C++, Default, Parameterized and Copy constructors.

What is constructor and why it is used?

Constructors, which concretely use a single class to create objects and return a new instance of the class, are abstracted by factories, which also create objects but can do so in various ways, using multiple classes or different allocation schemes such as an object pool.

Can you have 3 constructors in Java?

The technique of having two (or more) constructors in a class is known as constructor overloading. A class can have multiple constructors that differ in the number and/or type of their parameters. It's not, however, possible to have two constructors with the exact same parameters.


2 Answers

The constructors can appear anywhere in the code for the class. However, by convention, most people put them before any other functions that aren't constructors.

As for the variable names, all 6 are actually variable names, but the scope is differnet. The ones specified as parameters to the constructor (startCadence, startSpeed, startGear) are only available within the constructor. The other 3 (gear, cadence, speed) are probably class-wide variables, available to all methods. However the definition isn't shown in your code snippet. The full class would look mroe like:

class Bicycle {     // class-level variables     private int gear;     private int cadence;     private int speed;      // constructor     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;         cadence = startCadence;         speed = startSpeed;     }      // another method (not a constructor)     public void ShiftUp() {         gear = gear + 1; // notice the 'gear' variable is available here too.     } } 

Hope that helps!

like image 139
CodingWithSpike Avatar answered Sep 22 '22 05:09

CodingWithSpike


gear, cadence and speed are member variables of the class (declared elsewhere) and startCadence, startSpeed, and startGear are function parameters.

class Bicycle {     private int gear, cadence, speed;      public Bicycle(int startCadence, int startSpeed, int startGear)     {         // set the value of member variables from passed parameters         gear = startGear;         cadence = startCadence;         speed = startSpeed;      } } 
like image 36
basszero Avatar answered Sep 23 '22 05:09

basszero