Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Pass by value

- When you pass primitive values as arguments to a method, they are passed by value.
- copy of the actual value of the argument is passed to the method rather than the original variable
itself.

public class Lab475 {

public static void main(String[] args) {


int i = 10;
System.out.println((“Before change”+i);
change(i);
System.out.println(“After change”+i);
}

static void change(int a) {


a = 50;
}
}

Pass by reference
- When you pass an object to a method in Java, you are passing a copy of the reference (memory
address) to that object.
- Since the reference points to the same object in memory, modifications made to the object's state
inside the method are reflected outside the method as well.

public class Lab476 {

public static void main(String[] args) {


B b = new B();
System.out.println(“Before change”+b.i);
change(b);
System.out.println(“After change”+b.i);
}
static void change(B b) {
b.i = 50;
}
}
class B {
int i = 10;
}

BZ Coder Training Center


Author: Aditya Singhania
99
this keyword
- this keyword refers to the current instance using which the constructor or method is called.
- It can be used only from instance context i.e. from instance blocks, constructors and instance
methods.
- It is used for following purposes:
a) Avoiding variable naming conflict
b) Constructor Chaining
c) Method chaining

a) Avoiding variable naming conflict


 Use this followed by the variable name to access an instance variable within a
method or constructor or block.
 This helps differentiate between local variables and instance variables with the
same name.

class Lab477 { class Lab478 {


public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.display(); a.display();
} }
} }
class A { class A {
int i = 10; void display() {
void display() { int i = 20;
System.out.println(i); System.out.println(i);
} }
} }
class Lab479 { class Lab480 {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.display(); a.display();
} }
} }
class A { class A {
int i = 10; int i = 10;
void display() { void display() {
int a = 20; int i = 20;
System.out.println(i); System.out.println(i);
System.out.println(a); }
} }
}

BZ Coder Training Center


Author: Aditya Singhania
100
class Lab481 { class Lab482 {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.display(); a.display();
} }
} }
class A { class A {
int i = 10; int i = 10;
void display() { void display() {
int i = 20; int i = 20;
System.out.println(this.i); System.out.println(i);
} System.out.println(this.i);
} }
}
class Lab483 { class Lab484 {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.display(); a.display();
} }
} }
class A { class A {
static int i = 10; static int i = 10;
void display() { static void display() {
System.out.println(this.i); System.out.println(this.i);
} }
} }
class Lab485 { class Lab486 {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.display(); a.display();
} }
} }
class A { class A {
int i = 10; static int i = 10;
{ {
int i = 30; int i = 30;
System.out.println(i); System.out.println(i);
System.out.println(this.i); System.out.println(this.i);
} }
} }
class Lab487 { class Lab488 {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(20); A a = new A(20);
a.display(); a.display();
} }
} }
class A { class A {
int i = 10; int i = 10;
BZ Coder Training Center
Author: Aditya Singhania
101
A(int a) { A(int i) {
i = a; i = i;
} }
void display() { void display() {
System.out.println(i); System.out.println(i);
} }
} }
class Lab489 { class Lab490 {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(20); A a = new A();
a.display(); System.out.println(a);
} a.display();
} }
class A { }
int i = 10; class A {
A(int i) { void display() {
this.i = i; System.out.println(this);
} }
void display() { }
System.out.println(i);
}
}
class Lab491 {
public static void main(String[] args) {
A a = new A();
System.out.println(a);
a.display();
}
}
class A {
void display() {
A a = this;
System.out.println(a);
}
}

b) Constructor Chaining
 Constructor chaining refers to the process of calling one constructor from
another constructor within the same class using the this() keyword.
 If present it must be the first statement inside a constructor.
 Call to a constructor using this() must be made inside only another constructor.
Syntax:
this([arguments]);  arguments optional
Example:
this();  calls no-arg (D.C) constructor
this(12);  calls one arg constructor which takes integer as parameter.
this(12,13);  calls 2-arg cons which takes 2 integers as parameters.

BZ Coder Training Center


Author: Aditya Singhania
102
class Lab492 { class Lab493 {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
} }
} }
class A { class A {
A() { A() {
this(12); System.out.println(“A->D.C.”);
System.out.println(“A->D.C.”); }
} A(int i) {
A(int i) { this();
System.out.println(“A->1-arg cons”); System.out.println(“A->1-arg cons”);
} }
} }
class Lab494 { class Lab495 {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
} }
} }
class A { class A {
A(int i) { A() {
this(); System.out.println(“A->D.C.”);
System.out.println(“A->1-arg cons”); }
} A(int i) {
} System.out.println(“A->1-arg cons”);
this();
}
}
class Lab496 {
public static void main(String[] args) {
A a = new A();
}
}
class A {
A() {
System.out.println(“A->D.C.”);
}
A(int i) {
System.out.println(“A->1-arg cons”);
}
void display() {
this();
}
}

BZ Coder Training Center


Author: Aditya Singhania
103
c) Method Chaining
 Method chaining refers to the practice of calling multiple methods on an object
in a single line by chaining their invocations together.
 This technique is achieved by having each method return the object itself (this)
after performing its operations.
 The subsequent calls to the methods are made using same object which is
returned by this keyword.

class Lab497 { class Lab498 {


public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.display(); a.display().show().hello();
a.show(); }
a.hello(); }
} class A {
} public A display() {
class A { System.out.println(“display()”);
public void display() { return this;
System.out.println(“display()”); }
} public A show() {
public void show() { System.out.println(“show()”);
System.out.println(“show()”); return this;
} }
public void hello() { public void hello() {
System.out.println(“hello()”); System.out.println(“hello()”);
} }
} }
class Lab499 {
public static void main(String[] args) {
A a = new A();
a.display().show().hello().hai();
}
}
class A {
public A display() {
System.out.println(“display()”);
return this;
}
public A show() {
System.out.println(“show()”);
return this;
}
public void hello() {
System.out.println(“hello()”);
}
public void hai() {
System.out.println(“hai()”);
}
BZ Coder Training Center
Author: Aditya Singhania
104
}

Class Declaration:
- The file which contains programs are also known as source files.
- And the code written inside source file is also known as source code.
- A java source file mostly contains classes or interfaces.
- Following are the rules pertaining to java source files:
a) A java source file can contain any number of non-public classes or interfaces.
b) A java file can contain at most one public class.
c) If it contains one public class the source file name should be same as public class name.
d) If it does not contain any public class then we can give any name to source file.

Compiling and running java source file from CLI (Command Line Interface):

1) Compiling java source file in default package


- javac command is used to compile a java source file.
Syntax:
javac [path]file1.java [path]file2.java…..[path]fileN.java
- To compile a java program written in following location:
Example: C:\

BZ_Coder_Training

Java_programs

A.java

a) Either in command prompt the cwd should be:


C:\BZ_Coder_Training\Java_programs
and run it like:
C:\BZ_Coder_Training\Java_programs>javac A.java
b) Or provide the path to source file while compiling the code if CWD is not the path to the
java source file:
C:\> javac BZ_Coder_Training\Java_programs\A.java

Otherwise java won’t be able to locate the source file.


- Compiling programs using any approach will generate the .class file to the location where source
code file is present:
Example: C:\

BZ_Coder_Training

Java_programs

BZ Coder Training Center A.class


Author: Aditya Singhania
105
- The compilation of java source file the compiler generates separate .class files for all the classes in
the source code in CWD.

2) Running a .class file in default package


- java command is used to run a .class file containing main method in a core java application.
Syntax:
java file1
- To run a .class file the CWD should be the location where class file is present.
- To run a core java application we only provide the class name which has proper main method like:
public static void main(String[] args).
Example:
o To run this class file the current working directory must be
C:\BZ_Coder_Training\Java_programs
o Run the class file as:
C:\BZ_Coder_Training\Java_programs>java A

Packages:
- Packages are related classes grouped together in a single unit.
- Packages organize classes and interfaces into different namespaces, preventing naming conflicts by
allowing classes with the same name to coexist as long as they are in different packages.
- To use class from another package we use fully qualified class name.
- Fully Qualified Classname = packagename.classname
- To be accessible from another package a class and its members must be declared as public.
- To declare that a Java class belongs to a specific package, the package statement is used at the
beginning of the source file.
Syntax:
package <[.separated]pkgName>;
- If we don’t provide package statement then the class is considered to be in default package.
- Packages are generated as directories, but they are not directories.

Compiling classes in packages:


o Different version of javac command is used to generate packages and classes related to
those packages.
Syntax:
javac -d <path_to_package> [path_of_source_file]<file1.java>…[path_of_source_file]fileN.java
o -d in above syntax refers to the directory where the package should be generated and
[path_to_package] represents its value.
o The possible values for path_to_package can be one of the following:
1) Relative path:
This compiles A.java and places the generated class files in the bin directory
javac -d bin A.java  within the current working directory.
2) Absolute path:
This compiles A.java and places the generated class files in
javac -d /path/to/output A.java the specified absolute path.
3) Current directory:
javac -d . A.java This compiles A.java and places the generated class files in
the current working directory.

BZ Coder Training Center


Author: Aditya Singhania
106
Running classes in packages:
o To run classes in packages, point current working directory to path where the package is
generated
Example:
If A.class is generated in package abc.package1 at
C:\BZ_Coder_Training\Java_programs
as C:\BZ_Coder_Training\Java_programs\abc\package1\A.class
Then in command prompt cwd should be till-C:\BZ_Coder_Training\Java_programs
Run the class using fully qualified class name as:
C:\BZ_Coder_Training\Java_programs\abc>java abc.package1.A

package mypkg; //save this in Lab502.java


package mypkg;
class Lab500 { class Lab502 {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(“Main”); mypkg1.A a = new mypkg1.A();
} }
//create directory in C: drive as BZ_Coder_Training, inside it create }
Java_programs and save Lab500.java in it
//To compile, try various javac options as
// a) javac -d . Lab500.java  generates package in cwd //save this class in A.java
// b) javac -d ... Lab500.java  generates package mypkg in parent of cwd
// Create one more directory inside C: drive package_test package mypkg1;
//c) javac -d c:\package_test Lab500.java  absolute path class A {
package bzc.mypkg;
}
class Lab501 {
public static void main(String[] args) {
System.out.println(“Main”);
}
//create directory in C: drive as BZ_Coder_Training, inside it create
Java_programs and save Lab500.java in it
//To compile, try various javac options as
// a) javac -d . Lab500.java  generates package in cwd
// b) javac -d ... Lab500.java  generates package bzc.mypkg in parent of
cwd
// Create one more directory inside C: drive package_test
//c) javac -d c:\package_test Lab500.java  absolute path

//save this in Lab503.java //save this in Lab504.java


package mypkg; package mypkg;
class Lab503 { class Lab504 {
public static void main(String[] args) { public static void main(String[] args) {
mypkg1.A a = new mypkg1.A(); mypkg1.A a = new mypkg1.A();
} System.out.println(a.i);
} }
}
//save this class in A.java
package mypkg1; //save this class in A.java
public class A { package mypkg1;
public class A {

BZ Coder Training Center


Author: Aditya Singhania
107
} int i = 20;
}
//save this in Lab505.java //save this in Lab506.java
package mypkg; package mypkg;
class Lab505 { class Lab506 {
public static void main(String[] args) { public static void main(String[] args) {
mypkg1.A a = new mypkg1.A(); mypkg1.A a = new mypkg1.A();
System.out.println(a.i); System.out.println(a.i);
} }
} }

//save this class in A.java //save this class in A.java


package mypkg1; package mypkg1;
public class A { public class A {
public int i = 20; public int i = 20;
} }
//save this in Lab507.java //save this in Lab508.java
class Lab507 { class Lab508 {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
} System.out.println(a.i);
} }
}
//save this class in A.java
class A { //save this class in A.java
public class A {
} public int i = 20;
}

Importing packages
- When we have use classes from one package to another package we give fully-qualified class name.
- If we have to use those classes at many places in our code then the readability of the program is
reduced and writing fully qualified class names can becomes cumbersome and time consuming too.
- To improve readability, and reduce typing effort import statements are used.
- import statement is used to access classes and interfaces from other packages.
- It simplifies code by allowing you to reference classes without typing their fully qualified names
every time you use them.
- If a source file contains package statement, then import statements must be the next statement
after it.
- If a source file does not contain package statement, then import statements must be written first.
Syntax:
import packagename.classname;  imports only one class
import packagename.*;  imports all the classes from the package

BZ Coder Training Center


Author: Aditya Singhania
108
//save this in Lab509.java //save this in Lab510.java
package mypkg1; package mypkg1;

class Lab509 { import mypkg2.A;


public static void main(String[] args) {
mypkg2.A a = new mypkg2.A(); class Lab510 {
System.out.println(a.i); public static void main(String[] args) {
} A a = new A();
} System.out.println(a.i);
}
//save this class in A.java }
package mypkg2;
public class A { //save this class in A.java
public int i = 20; package mypkg2;
} public class A {
public int i = 20;
}
//save this in Lab511.java //save this in Lab512.java
package mypkg1; package mypkg1;

import mypkg2.A; import mypkg2.A;

class Lab511 { class Lab512 {


public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
B b = new B(); mypkg2.B b = new mypkg2.B();
System.out.println(a.i); System.out.println(a.i);
System.out.println(b.j); System.out.println(b.j);
} }
} }

//save this class in A.java //save this class in A.java


package mypkg2; package mypkg2;
public class A { public class A {
public int i = 20; public int i = 20;
} }

//save this class in B.java //save this class in B.java


package mypkg2; package mypkg2;
public class B { public class B {
public int j = 100; public int j = 100;
} }

BZ Coder Training Center


Author: Aditya Singhania
109
//save this in Lab512.java //save this in Lab513.java
package mypkg1; class Lab513 {
public static void main(String[] args) {
import mypkg2.*; A a = new A();
B b = new B();
class Lab512 { System.out.println(a.i);
public static void main(String[] args) { System.out.println(b.j);
A a = new A(); }
B b = new B(); }
System.out.println(a.i);
System.out.println(b.j); //save this class in A.java
} package mypkg2;
} public class A {
public int i = 20;
//save this class in A.java }
package mypkg2;
public class A { //save this class in B.java
public int i = 20; package mypkg2;
} public class B {
public int j = 100;
//save this class in B.java }
package mypkg2;
public class B {
public int j = 100;
}
//save this in Lab514.java
import mypkg2.*;
class Lab514 {
public static void main(String[] args) {
A a = new A();
B b = new B();
System.out.println(a.i);
System.out.println(b.j);
}
}

//save this class in A.java


package mypkg2;
public class A {
public int i = 20;
}

//save this class in B.java


package mypkg2;
public class B {
public int j = 100;
}

BZ Coder Training Center


Author: Aditya Singhania
110

You might also like