0% found this document useful (0 votes)
22 views7 pages

Objects

Objects in Java are instances of classes. A new object is created using the new keyword, which calls the constructor of that class. The constructor initializes the object and has no return type. Fields and initializer blocks are initialized in the order they appear in the class, before the constructor runs. This allows initializing fields before they are accessed in the constructor. The name of a constructor should start with an uppercase letter by convention, though compilers allow lowercase as well.

Uploaded by

abilgen006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Objects

Objects in Java are instances of classes. A new object is created using the new keyword, which calls the constructor of that class. The constructor initializes the object and has no return type. Fields and initializer blocks are initialized in the order they appear in the class, before the constructor runs. This allows initializing fields before they are accessed in the constructor. The name of a constructor should start with an uppercase letter by convention, though compilers allow lowercase as well.

Uploaded by

abilgen006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Building Blocks

Objects
Creating objects
• object is an instance of the class (house → blueprint)
• new object is created using a keyword new :

Student s = new Student()

• when object is created, the constructor of the object is called


// in file Student.java the constructor
public class Student { (like method, but no return type)
public Student() {

System.out.println("New student is created.");

$ java MyApp
// in file MyApp.java
New student is created.
public class MyApp {

public static void main(String[] args) {

Student s = new Student();

} new object is created


} and constructor is called
// will this compile?

public class Student { return type


public void Student() {

System.out.println("New student is created.");

// YES!

// But here Student() is just a method, not a constructor

// (it will not be called when new object is created)

// good practice is to write methods with lowercase first letter

// (but exam creators like these kind of practical jokes)


// if you don't provide any constructor, the compiler will generate

// simple no-argument constructor: public Student() { }

// reading and modifying fields:

public class Student {

String name; // instance variable

public static void main(String[] args) {

Student s = new Student(); // creating an object

s.name = "John Wayne"; // set variable

System.out.println(s.name); // get variable

}
Order of initialization
• the code between two brackets {...} is called code block
• instance initializer - code block outside the method
• order of initialization:
1. elds and instance initializer blocks in order in which they appear

2. constructor runs in the end


fi
public class Dog {

private String name = "Chip";

public Dog() { 2
name = "Teddy";

System.out.println("Inside the constructor...");

} 1
{ System.out.println("Inside the initializer block..."); }

public static void main(String[] args) {

Dog dog = new Dog(); 3 $ java Dog


System.out.println(dog.name); Inside the initializer block...
Inside the constructor...
} Teddy

You might also like