Professional Documents
Culture Documents
Instantiating Variables: 1) Directly 2) Through Setter 3) Constructor
Instantiating Variables: 1) Directly 2) Through Setter 3) Constructor
1)Directly
2) Through setter
3)constructor
Instantiating variables Examples
Class A {
Int x=100;
Int y=200;
Void display() {
System.out.println(“x:”+x+”y:”+y); }
Public static void main(String[] args) {
A a1 = new A();
a1.display();
A a2 = new A();
a2.display(); }}
Instantiating variables through method
Class A {
int x;
int y;
void setXY(int x, int y) { this.x=x
this.y=y; }
void display() {
System.out.println(“x:”+x+”y:”+y); }
Public static void main(String[] args) {
A a1 = new A();
a1.setXY(100,200);
a1.display();
A a2 = new A();
a2.setXY(400,500);
a2.display(); }}
Instantiating variables through constructor
Class A {
int x;
int y;
A(int x, int y) { this.x=x
this.y=y; }
void display() {
System.out.println(“x:”+x+”y:”+y); }
Public static void main(String[] args) {
A a1 = new A(100,200);
//a1.setXY(100,200);
a1.display();
A a2 = new A(400,500);
//a2.setXY(400,500);
a2.display(); }}
Method calls flow
Object memory allocation and method call
in stack
package
• A package as the name suggests is a
pack(group) of classes, interfaces and other
packages. In java we use packages to organize
our classes and interfaces. We have two types
of packages in Java: built-in packages and the
packages we can create (also known as user
defined package).
• Advantages: reusability, name conflicts, better
organisation
package
• Reusability: While developing a project in java, we often feel
that there are few things that we are writing again and again in
our code. Using packages, you can create such things in form
of classes inside a package and whenever you need to perform
that same task, just import that package and use the class.
• Better Organization: Again, in large java projects where we
have several hundreds of classes, it is always required to group
the similar types of classes in a meaningful package name so
that you can organize your project better and when you need
something you can quickly locate it and use it, which improves
the efficiency.
• Name Conflicts: We can define two classes with the same
name in different packages so to avoid name collision, we can
use packages
In Build package
• import java.util.Scanner
• Here:
→ java is a top level package
→ util is a sub package
→ and Scanner is a class which is present in
the sub package util.
• java.io.*, java.lang.*
User defined Package examples
This statement should be the first statement in the
class.
Package com.murali.training;
Class PackageTest {
Int x=100;
Void display() {
System.out.println(“x::”+x);
}}
package
• Sometimes class name conflict may occur
then we have to use fully qualified name.
• Import com.abc.xyz;
• Import com.training.java;
• In the above we have common class Test then
we have to use fully qualified name of the
class.
Usage of package best practice
• Import exact the class which is required
instead of importing all the classes in that
package.
• Import com.abc.xyz.*;
• Use this, instead of above import
• Import com.abc.xyz.Test;
Access modifiers
• An access modifier restricts the access of a class,
constructor, data member and method in
another class. In java we have four access
modifiers:
1. default
2. private
3. protected
4. public
default access modifier
• When we do not mention any access modifier,
it is called default access modifier.
package com.abc.xyz;
package com.xyz.test;
import com.abc.test. Addition ;
class Test extends Addition{
public static void main(String args[]){
Test obj = new Test();
System.out.println(obj.addTwoNumbers(11, 22));
}
}
Public access modifier
package com.abc.pqr;
import com.abc.xyz.*;
class Test{
public static void main(String args[]){
Addition obj = new Addition();
System.out.println(obj.addTwoNumbers(100, 1));
}
}
The scope of access modifiers