Creating Objects
How to create object?
Park p = new Park();
What is constructor?
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.
Two key points to note about the constructor
2 key points to identify a constructor.
public class Chick {
public Chick() {
System.out.println("in constructor");
}
}Default Constructor
For most classes, you don’t have to code a constructor—the compiler will supply a “do nothing” default constructor for you.
The purpose of a constructor
is to initialize fields
READING AND WRITING MEMBER FIELDS
public class Swan {
int numberEggs; // instance variable
public static void main(String[] args) {
Swan mother = new Swan();
mother.numberEggs = 1; // set variable
System.out.println(mother.numberEggs); // read variable
}
read values of already initialized fields on a line initializing a new field
1: public class Name {
2: String first = "Theodore";
3: String last = "Moose";
4: String full = first + last;
5: }EXECUTING INSTANCE INITIALIZER BLOCKS
Code Block
The code between the braces is called a code block.(sometimes called “inside the braces” {})
Instance Initializer
Code blocks appear outside a method.
Line 5 is an instance initializer
1: public class Bird {
2: public static void main(String[] args) {
3: { System.out.println("Feathers"); }
4: }
5: { System.out.println("Snowy"); } //instance initializer
6: }FOLLOWING ORDER OF INITIALIZATION
Order of Initialization
What is the output?
1: public class Chick {
2: private String name = "Fluffy";
3: { System.out.println("setting field"); }
4: public Chick() {
5: name = "Tiny";
6: System.out.println("setting constructor");
7: }
8: public static void main(String[] args) {
9: Chick chick = new Chick();
10: System.out.println(chick.name); } }Ans : setting field setting constructor Tiny
1: public class Chick {
2: private String name = "Fluffy"; //2
3: { System.out.println("setting field"); }//3
4: public Chick() {
5: name = "Tiny";//4
6: System.out.println("setting constructor");//5
7: }
8: public static void main(String[] args) {
9: Chick chick = new Chick(); //1
10: System.out.println(chick.name); } }//6Can you refer to a variable before it has been defined?
Ans : No.
Order matters for the fields and blocks of code. You can’t refer to a variable before it has been defined
Example :
{ System.out.println(name); } // DOES NOT COMPILE
private String name = "Fluffy";What do you think this code prints out?
public class Egg {
public Egg() {
number = 5;
}
public static void main(String[] args) {
Egg egg = new Egg();
System.out.println(egg.number);
}
private int number = 3;
{ number = 4; }
Ans: 5
public class Egg {
public Egg() {
number = 5; //4
}
public static void main(String[] args) {
Egg egg = new Egg(); //1
System.out.println(egg.number);
}
private int number = 3; //2
{ number = 4; } //3
}Data types
What is primitive types?
Balanced parentheses problem
You cannot use a closed brace “}” if there’s no corresponding open brace “{” that it matches written earlier in the code.
In programming, this is referred to as the balanced parentheses problem, and it often comes up in job interview questions.
Java primitive types
IS STRING A PRIMITIVE?
Ans : No.
What is String default value?
String or any object default value is null.
Java primitive type key points:
SIGNED AND UNSIGNED: SHORT AND CHAR
short and char are closely related, as both are stored as integral types with the same 16-bit length.
short is signed
char is unsigned
Unsigned means range is strictly positive including 0.
Therefore, char can hold a higher positive numeric value than short, but cannot hold any negative numbers.
Valid short and char declaration
short bird = 'd'; char mammal = (short)83; System.out.println(bird); // Prints 100 System.out.println(mammal); // Prints S
Invalid short and char declaration
short reptile = 65535; // DOES NOT COMPILE out of range char fish = (short)-1; // DOES NOT COMPILE out of range