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

Note:

# Optional specifier and access specifier can be interchanged

# But return type and optional specifier cannot be interchanged

Ex:

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.

Ex:

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)

Ex:

package chapter4;

public class ex1 {

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

System.out.println(nums.length);

}

public static void main(String[] args) {

walk(1);

walk(1, 2);

walk(1, 2, 3);

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

walk(1, null);

}

}

Output

0

1

2

3

Exception in thread “main” java.lang.NullPointerException

at chapter4.ex1.walk(ex1.java:6)

at chapter4.ex1.main(ex1.java:15)

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

Ex:

package chapter4;

public class ex2 {

public static final int NUM_BUCKETS = 45;

public static void main(String[] args) {

NUM_BUCKETS = 50;

}

}

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

Ex:

package chapter4;

import java.util.ArrayList;

public class ex3 {

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

 

public static void main(String[] args) {

values.add(“Changed”);

}

}

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

Ex:

package chapter4;

public class ex4 {

final float PI;

{

PI = 3.14f;

}

}

#3: Static Initializer Block

Ex:

package chapter4;

public class ex5 {

static final float PI;

static {

PI = 3.14f;

}

}

Ex:

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;

Rules

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

Ex:

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

asList(“one”);//IT COMPILES

#2 importing same name gives compiler error

Ex:

import static statics.A.TYPE;

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

Overloading
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

Constructor

# 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

Ex:

package chapter4;

public class ex8_2 {

public ex8_2(int weight) {

new ex8_2(weight, “brown”);

}

public ex8_2(int weight, String color) {

this(weight);

}

}

# this() must be the first statement in constructor

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s