Chapter 4 – Methods and Encapsulation

Access Modifiers

# public – any class

# private – same class only

# protected – same class, sub classes in the same and other package

# default – package private (within package)

Ex: default void walk(){}//does not compile

In the above line, default is not a valid access modifier

#default method is only allowed in interface

Optional Specifier

# can have zero or more access specifier

Ex: static, final, abstract, synchronized, native, strictfp


# Optional specifier and access specifier can be interchanged

# But return type and optional specifier cannot be interchanged


public void final dummy(){}//DOES NOT COMPILE

final public void dummy(){}//IT COMPILES

Return Type

# void – no return type, omit return or give return statement and without value.


public void dummy(){return;}

public String walk(int a){if(a==4) return ””;}//DOES NOT  COMPILE

Working with Varargs

# different than array

# a varargs parameter must be last element in method signature

# implies only one varargs parameter per method

# pass an array or list the elements of the array or omit the values (creates array of length zero)


package chapter4;

public class ex1 {

public static void walk(int start, int… nums) {



public static void main(String[] args) {


walk(1, 2);

walk(1, 2, 3);

walk(1, new int[] { 2, 4, 5 });

walk(1, null);








Exception in thread “main” java.lang.NullPointerException

at chapter4.ex1.walk(

at chapter4.ex1.main(

Static Methods

# main can be called like any other static methods

# static methods have two main purposes:

■ For utility or helper methods that don’t require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.

■ For state that is shared by all instances of a class, like a counter. All instances must share the same state. Methods that merely use that state should be static as well.

Static Variable

# when we update a final variable of primitive type, compiler error is thrown


package chapter4;

public class ex2 {

public static final int NUM_BUCKETS = 45;

public static void main(String[] args) {




# a final variable of reference type are allowed to call methods


package chapter4;

import java.util.ArrayList;

public class ex3 {

public static final ArrayList<String> values = new ArrayList<>();


public static void main(String[] args) {




Static Initialization

# static initializer runs when class is first used

# statements in it run and assign a static variable as needed.

Ways of initializing final variable

#1: Direct initialization

final float PI=3.14f; static final float PI=3.14f;

#2: Instance Initializer Block


package chapter4;

public class ex4 {

final float PI;


PI = 3.14f;



#3: Static Initializer Block


package chapter4;

public class ex5 {

static final float PI;

static {

PI = 3.14f;




package chapter4;

public class ex6 {

private static int one;

private static final int two;

private static final int three = 3;

private static final int four; // DOES NOT COMPILE

static {

one = 1;

two = 2;

three = 3; // DOES NOT COMPILE

two = 4; // DOES NOT COMPILE



Static Import

# its for importing static members.

Syntax: import static java.util.Arrays.asList;


#1 if you are importing static member, you should directly access static variable,


Arrays.asList(“one”);//DOES NOT COMPILE

asList(“one”);//IT COMPILES

#2 importing same name gives compiler error


import static statics.A.TYPE;

import static statics.B.TYPE; // DOES NOT COMPILE

Rules for method signature

#1 change in type/order/number of arguments

#2 return type not considered

#3 varargs overloading

Ex: both the lines are same,

public void fly(int[] args){}

public void fly(int… args){}

#4 Primitive level overloading follows the sequence,

  • Exact match
  • If exact match not found, then autoboxing
  • If autoboxing unavailable, then Object
  • If Object unavailable, convert to high level


# if constructor is private, we cannot create instance of the class in another class

# we cannot call constructor without new keyword

# can be called followed by new keyword


package chapter4;

public class ex8_2 {

public ex8_2(int weight) {

new ex8_2(weight, “brown”);


public ex8_2(int weight, String color) {




# this() must be the first statement in constructor


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s