Head First Java: Chapter-4 summary

Tharaka Dissanayake
5 min readJul 16, 2022

--

How Objects Behave

Classes describe how the JVM should make an object of that type. Instance variables represent the state of the object(knows), and methods represent the behaviour of that object.

Object structure

Every instance of a particular class has the same methods, but the methods can behave differently based on the value of the instance variables.

Sending things to the method.

Items that are passed to the method are called arguments. Receiving things to the method when calling it are called parameters. Parameters behave as a variable within the methods. A method uses parameters. A caller passes arguments.

The number and type of values you pass in must match the order and type of the parameters declared by the method.

Example.

The “sayHello” method takes a string value as a parameter.

The “sayHello” method takes a string value as a parameter.

That sayHello method can be called through the object. When calling that method, we must define the arguments in the parenthesis.

Main class

Then the following result will be received.

Result of the program.

The above example mentions one parameter in the method. But methods can take many parameters. Then parameters must be separated by a comma.

Many parameters can be allowed by the method.

Getting things back from a method.

Methods can give things back. It is called return values. When declaring a method, the return value will be mentioned.

The “void” represent that method; nothing gives back. (No return value). If there is a return value from the method, the type of that value must be represented on the method signature.

Values passed in and out of methods can be implicitly promoted to a larger type or explicitly cast to a smaller type.

Only one return value can be taken from the method. Also, methods can return arrays.

There should be one written for the method.

Java is pass-by-value. That means pass-by-copy

Declare a variable x and pass it into the “getSqure” method. In this process, the x value is not passed to the method. First, the x value is copied, and that copied value will be passed to the method as an argument. So if the method does something to, the copied, original x value is not affected.

Getters and Setters

The good programmer did not directly access instance variables outside of the class. So “Getters” and “Setters” are also methods used to get and set values for the instance variables.

Why we need gettwers and setters.?

Values of the instance variables can be accessed outside of the class using the dot (.) operator. That is called variables exposed.

As an example, there is a “Student” class. A student with 50 or greater than 50 marks for maths will be eligible for the course. Instead of setting the “isElgible” variable directly, it can be done through a setter before setting its value, maths mark can be checked.

Student class

The above example (Student class) before the instance variable has a keyword as private. It is a kind of access modifier. Also, there are public access modifiers for getters and setters. The private access modifiers say that method or variables can be accessed within only that class. The public access modifiers allow everyone to access that method or instance variable.

Instance variables must be private, and getters and setters must be public.

Encapsulation

Encapsulation is bundling the data and methods that operate on that data into a single unit. That single unit is called class.

Encapsulation puts a force-field around instance variables, so nobody can set them to, let’s say, something inappropriate.

How do objects in an array behave?

Array with objects

Declaring variables means providing a name and type to the variable. Initialization variable means to assign value to that declared variable.

What will happen when calling instance variables without initializing them? It provides default values because JVM will assign default values to the instance variables when running the program.

Defaults value for variables.

The difference between instance and local variables

Instance variables are declared inside a class but not within a method. Local variables are declared within a method. Local variables MUST be initialized before use; otherwise, the compiler gives compile time error. (“variable might not have been initialized”).

Method parameters behave as local variables. They are declared inside the method but not declared within the method body. Method parameters will never be uninitialized, so the compiler does not give errors like local variables because When calling that method compiler to check the arguments. So parameters are ALWAYS initialized because the compiler guarantees that methods are always called with arguments that match the parameters declared for the method, and the arguments are assigned (automatically) to the parameters.

Comparing variables.

“==” sign can be used to compare primitive variables. “==” compare the bits that variable represents.

The reference type “==” is used to compare the two objects that refer to the same object. If you want to compare if two different objects are equal. Then .equals() must be used.

--

--

Tharaka Dissanayake
Tharaka Dissanayake

No responses yet