// Creation of an Object
public class Main22 {
int x = 5;
public static void main(String[] args) {
Main22 myObj = new Main22();
System.out.println(myObj.x);
}
}
Output:
//Multiple Objects Cretion
public class Main23 {
int x = 5;
public static void main(String[] args) {
Main23 myObj1 = new Main23(); //
Object 1
Main23 myObj2 = new Main23(); //
Object 2
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}
}
Output:
//Using Multiple Classes
public class Main24 {
int x = 5;
}
class Second {
public static void main(String[] args) {
Main24 myObj = new Main24();
System.out.println(myObj.x);
}
}
Output:
Javac Main24.java
Javac Second.java
Java Second
//Example: Java Class and Objects
class Lamp {
// stores the value for light
// true if light is on
// false if light is off
boolean isOn;
// method to turn on the light
void turnOn() {
isOn = true;
System.out.println("Light on? " + isOn);
// method to turnoff the light
void turnOff() {
isOn = false;
System.out.println("Light on? " + isOn);
}
}
class Main25 {
public static void main(String[] args) {
// create objects led and halogen
Lamp led = new Lamp();
Lamp halogen = new Lamp();
// turn on the light by
// calling method turnOn()
led.turnOn();
// turn off the light by
// calling method turnOff()
halogen.turnOff();
}
}
Output:
//Example: Create objects inside the same
class
class Lamp {
// stores the value for light
// true if light is on
// false if light is off
boolean isOn;
// method to turn on the light
void turnOn() {
isOn = true;
System.out.println("Light on? " + isOn);
public static void main(String[] args) {
// create an object of Lamp
Lamp led = new Lamp();
// access method using object
led.turnOn();
}
}
Output:
//Java Program to illustrate how to define a
class and fields
//Defining a Student class.
class Student{
//defining fields
int id;//field or data member or instance
variable
String name;
//creating main method inside the Student
class
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an
object of Student
//Printing values of the object
System.out.println(s1.id);//accessing
member through reference variable
System.out.println(s1.name);
}
}
Output:
//Java Program to demonstrate having the
main method in
//another class
//Creating Student class.
class Student12{
int id;
String name;
}
//Creating another class TestStudent1
which contains the main method
class TestStudent1{
public static void main(String args[]){
Student12 s1=new Student12();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
Output:
//1) Object and Class Example:
Initialization through reference
class Student{
int id;
String name;
}
class TestStudent2{
public static void main(String args[]){
Student s1=new Student();
s1.id=101;
s1.name="Sonoo";
System.out.println(s1.id+"
"+s1.name);//printing members with a
white space
}
}
Output:
class Student{
int id;
String name;
}
//Object and Class Example: Initialization
through reference
class TestStudent3{
public static void main(String args[]){
//Creating objects
Student s1=new Student();
Student s2=new Student();
//Initializing objects
s1.id=101;
s1.name="Sonoo";
s2.id=102;
s2.name="Amit";
//Printing data
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}
Output:
//2) Object and Class Example:
Initialization through method
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void
displayInformation(){System.out.println(r
ollno+" "+name);}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
Output:
//3) Object and Class Example:
Initialization through a constructor
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();
}
}
Output:
//Object and Class Example: Rectangle
class Rectangle{
int length;
int width;
void insert(int l, int w){
length=l;
width=w;
}
void
calculateArea(){System.out.println(length
*width);}
}
class TestRectangle1{
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
Output:
//Creating multiple objects by one type
only
//Java Program to illustrate the use of
Rectangle class which
//has length and width data members
class Rectangle{
int length;
int width;
void insert(int l,int w){
length=l;
width=w;
}
void
calculateArea(){System.out.println(length
*width);}
}
class TestRectangle2{
public static void main(String args[]){
Rectangle r1=new Rectangle(),r2=new
Rectangle();//creating two objects
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
Output:
//Real World Example: Account
//Java Program to demonstrate the working
of a banking-system
//where we deposit and withdraw amount
from our account.
//Creating an Account class which has
deposit() and withdraw() methods
class Account{
int acc_no;
String name;
float amount;
//Method to initialize object
void insert(int a,String n,float amt){
acc_no=a;
name=n;
amount=amt;
}
//deposit method
void deposit(float amt){
amount=amount+amt;
System.out.println(amt+" deposited");
}
//withdraw method
void withdraw(float amt){
if(amount<amt){
System.out.println("Insufficient Balance");
}else{
amount=amount-amt;
System.out.println(amt+" withdrawn");
}
}
//method to check the balance of the
account
void
checkBalance(){System.out.println("Balan
ce is: "+amount);}
//method to display the values of an object
void
display(){System.out.println(acc_no+"
"+name+" "+amount);}
}
//Creating a test class to deposit and
withdraw amount
class TestAccount{
public static void main(String[] args){
Account a1=new Account();
a1.insert(832345,"Ankit",1000);
a1.display();
a1.checkBalance();
a1.deposit(40000);
a1.checkBalance();
a1.withdraw(15000);
a1.checkBalance();
}}
Output:
// Class Declaration
public class Dog {
// Instance Variables
String name;
String breed;
int age;
String color;
// Constructor Declaration of Class
public Dog(String name, String breed,
int age,
String color)
{
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}
// method 1
public String getName() { return name;
}
// method 2
public String getBreed() { return breed;
}
// method 3
public int getAge() { return age; }
// method 4
public String getColor() { return color; }
@Override public String toString()
{
return ("Hi my name is " +
this.getName()
+ ".\nMy breed,age and color are
"
+ this.getBreed() + "," +
this.getAge()
+ "," + this.getColor());
}
public static void main(String[] args)
{
Dog tuffy
= new Dog("tuffy", "papillon", 5,
"white");
System.out.println(tuffy.toString());
}
}
Output:
public class GFG {
// sw=software
static String sw_name;
static float sw_price;
static void set(String n, float p)
{
sw_name = n;
sw_price = p;
}
static void get()
{
System.out.println("Software name is:
" + sw_name);
System.out.println("Software price is:
"
+ sw_price);
}
public static void main(String args[])
{
GFG.set("Visual studio", 0.0f);
GFG.get();
}
}
Output:
Class Object
Class is the blueprint of an object. It is
An object is an instance of the class.
used to create objects.
No memory is allocated when a class is Memory is allocated as soon as an object
declared. is created.
An object is a real-world entity such as a
A class is a group of similar objects.
book, car, etc.
Class is a logical entity. An object is a physical entity.
Objects can be created many times as per
A class can only be declared once.
requirement.
Objects of the class car can be BMW,
An example of class can be a car.
Mercedes, Ferrari, etc.
public class Puppy {
int puppyAge;
public Puppy(String name) {
// This constructor has one parameter,
name.
System.out.println("Name chosen is :"
+ name );
}
public void setAge( int age ) {
puppyAge = age;
}
public int getAge( ) {
System.out.println("Puppy's age is :" +
puppyAge );
return puppyAge;
}
public static void main(String []args) {
/* Object creation */
Puppy myPuppy = new Puppy(
"tommy" );
/* Call class method to set puppy's age
*/
myPuppy.setAge( 2 );
/* Call another class method to get
puppy's age */
myPuppy.getAge( );
/* You can access instance variable as
follows as well */
System.out.println("Variable Value :"
+ myPuppy.puppyAge );
}
}
Output:
import java.io.*;
public class Employee {
String name;
int age;
String designation;
double salary;
// This is the constructor of the class
Employee
public Employee(String name) {
this.name = name;
}
// Assign the age of the Employee to the
variable age.
public void empAge(int empAge) {
age = empAge;
}
/* Assign the designation to the variable
designation.*/
public void empDesignation(String
empDesig) {
designation = empDesig;
}
/* Assign the salary to the variable
salary.*/
public void empSalary(double
empSalary) {
salary = empSalary;
}
/* Print the Employee details */
public void printEmployee() {
System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" +
designation );
System.out.println("Salary:" + salary);
}
}
import java.io.*;
public class EmployeeTest {
public static void main(String args[]) {
/* Create two objects using constructor
*/
Employee empOne = new
Employee("James Smith");
Employee empTwo = new
Employee("Mary Anne");
// Invoking methods for each object
created
empOne.empAge(26);
empOne.empDesignation("Senior
Software Engineer");
empOne.empSalary(1000);
empOne.printEmployee();
empTwo.empAge(21);
empTwo.empDesignation("Software
Engineer");
empTwo.empSalary(500);
empTwo.printEmployee();
}
}
Output:
public class Main59 {
// Static method
static void myStaticMethod() {
System.out.println("Static methods can
be called without creating objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods
must be called by creating objects");
}
// Main method
public static void main(String[] args) {
myStaticMethod(); // Call the static
method
// myPublicMethod(); This would
compile an error
Main59 myObj = new Main59(); //
Create an object of Main
myObj.myPublicMethod(); // Call the
public method on the object
}
}
Output: