- Objects
- Classes
- Inheritance
-
Programming in Java on Your Computer - Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Scope
- Inheritance
-
BlueJ Review - Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Constructor Code
Premium Content - Free Preview
We saw that all methods need return types or the word void
for no return type. However, there is one exception: constructors. This is because a constructor is called when an object is created, so it 'returns' the Object itself and cannot return anything else.
All about Constructors
A constructor is created by simply writing a method with the same name as the class itself (including the capitalization). The constructor is usually used to set up the values of variables in the object and run any methods that need to be run on creation. Like any method, a constructor can take any number of parameters. As mentioned, a class can have multiple constructors as long as each one has different parameters.
End of Free Content Preview. Please Sign in or Sign up to buy premium content.
Comments
Honzis
Dec 23, 8:20 AMWhy is this constructor returning false for the sunroof of car2? ... Car(String clr, boolean roof){
color = clr;
roof = sunroof;
//add 2 parameters above and setup their values
}
Honzis
Dec 23, 8:21 AMSorry, I found it :)
Lance
Feb 10, 8:31 PMI did the same thing :P
Jake
Jun 28, 6:51 PMyou need to put the variable sunroof before the variable roof
color = clr;
sunroof = roof;
Learneroo
Jun 28, 10:41 PM@Jake, the order of the variable assignment does not matter. (Though the constructor needs to take in the String first in the parameters.)
Mandy
Oct 19, 12:14 PMhow do I tackle this?
Learneroo
Oct 19, 12:23 PMyou need to set
color
andsunroof
in the constructors (where the comments are).Mandy
Oct 19, 12:26 PMhow can I correct this?
Car(String clr){
color=clr;
//add 1 parameter above and setup value here
}
Car(boolean roof){
sunroof=roof;
Learneroo
Oct 19, 12:33 PMThe second constructor should also take in a variable for
color
and assign it.Mandy
Oct 19, 12:39 PMthanks!
Raz Levi
Dec 4, 7:59 AMmy code
What is not correct?
Learneroo
Dec 4, 9:49 AMYou first need to fix all the compile errors. See the hint I added to get started and simply assign the Car's variables.
Raz Levi
Dec 6, 7:30 PMwhy its show me a compile error?
("Main.java:33: error: reached end of file while parsing
}
")
Learneroo
Dec 6, 8:09 PMThat usually means you're missing an
}
. You can write some of the code for these challenges in BlueJ (or another IDE) so you can see compile errors quickly.Raz Levi
Dec 6, 8:23 PMhttp://www.learneroo.com//modules/18/nodes/133#com974&togetherjs=NpNuPuKxsj
What is not correct please??
Learneroo
Dec 6, 8:37 PMYou're pretty close, but you need to make sure you're taking the value of the parameter and assigning it to the instance variable.
instanceVariable = parameter;
.The instance variable (declared at the beginning of the class) is on the left side, while the parameter (passed in through the parentheses) is on the right side.
See this solution for the full code.
thales
Jul 8, 2:07 PMmy code