Java Programming - Classes
Classes: Class Fundamentals - objects – Assigning Object Reference Variables
Class Fundamentals
2 november
General Form of a Class
Simple class
2 november
Object Creation of a Class
Class
Object creation
Reference Variable-declare reference to object
2 november
Box mybox = new Box();
mybox.width = 100;
Box mybox;
Create a Class
Object Creation
2 november
public class Main
{
int x = 5;
}
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Multiple Object Creation
2 november
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj1 = new Main(); // Object 1
Main myObj2 = new Main(); // Object 2
System.out.println(myObj1.x);
System.out.println(myObj2.x);
} }
Using Multiple Classes
2 november
public class Main {
int x = 5;
}
class Second {
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Java Class Attributes
Accessing Attributes
2 november
public class Main
{
int x = 5;
int y = 3;
}
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Modify Attributes
Final Keyword
2 november
public class Main {
int x=10;
public static void main(String[] args) {
Main myObj = new Main();
myObj.x = 40;
System.out.println(myObj.x);
} }
public class Main {
final int x = 10;
public static void main(String[] args) {
Main myObj = new Main();
myObj.x = 25; // will generate an error: cannot assign a value to a final variable
System.out.println(myObj.x);
} }
Object Creation of a Class
Object creation
Output
2 november
Box mybox = new Box();
mybox.width = 100;
Volume is 3000.0
Output
Volume is 3000.0
Volume is 162.0
Assigning Object Reference Variables
Box b1 = new Box();
Box b2 = b1;
b1 and b2 both refer to the same object, they are not linked in any other way.
Introducing Methods –Constructors –this keyword –Garbage Collection–Stack Class.
Methods
Methods
return value;
Adding a Method to the Class
Volume is 3000.0
Volume is 162.0
Returning a Value
Method with Parameters
public class Main {
static void myMethod(String fname) {
System.out.println(fname + " Refsnes");
}
public static void main(String[] args) {
myMethod("Liam");
myMethod("Jenny");
myMethod("Anja");
}
}
Multiple Parameters
public class Main {
static void myMethod(String fname, int age) {
System.out.println(fname + " is " + age);
}
public static void main(String[] args) {
myMethod("Liam", 5);
myMethod("Jenny", 8);
myMethod("Anja", 31);
}
}
Static vs. Non-Static Methods
public class Main {
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
public static void main(String[] args) {
myStaticMethod();
Main myObj = new Main();
myObj.myPublicMethod();
}
}
Note: static method, which means that it can be accessed without creating an object of the class
Using Multiple Classes
public class Main {
public void fullThrottle() {
System.out.println("The car is going as fast as it can!");
}
public void speed(int maxSpeed) {
System.out.println("Max speed is: " + maxSpeed);
} }
class Second {
public static void main(String[] args) {
Main myCar = new Main();
myCar.fullThrottle();
myCar.speed(200);
} }
Example : Student
class Student
{
int rollno;
String name;
void insertRecord(int r, String n)
{
rollno=r;
name=n;
}
void displayInformation(){System.out.println(rollno+" "+name);
}
class TestStudent{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Kavin");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
} }
Java Constructors
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes
public class Main {
int x;
public Main()
{
x = 5;
}
public static void main(String[] args)
{
Main myObj = new Main();
System.out.println(myObj.x); } }
Constructor Parameters
public class Main {
int x;
public Main(int y) {
x = y;
}
public static void main(String[] args) {
Main myObj = new Main(5);
System.out.println(myObj.x);
}
}
Many Parameters
public class Main {
int modelYear;
String modelName;
public Main(int year, String name)
{
modelYear = year;
modelName = name;
}
public static void main(String[] args) {
Main myCar = new Main(1969, "Mustang");
System.out.println(myCar.modelYear + " " + myCar.modelName); } }
Example : Employee
class Employee{
int id;
String name;
float salary;
void insert(int i, String n, float s) {
id=i;
name=n;
salary=s;
}
void display(){System.out.println(id+" "+name+" "+salary);}
}
public class TestEmployee {
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
e1.insert(101,"ajeet",45000);
e2.insert(102,"irfan",25000);
e3.insert(103,"nakul",55000);
e1.display();
e2.display();
e3.display();
} }
this Keyword
Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked.
this Keyword
this Keyword
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
class Student{
int rollno;
String name;
float fee;
Student(int r,String n,float f){
rollno=r;
name=n;
fee=f;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis3{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display(); }}
Without this Keyword
Garbage Collection
In traditional C++, dynamically allocated objects must be manually released by use of a delete operator. Java it handles deallocation automatically.
object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed. There is no need to explicitly destroy objects
Thank You