0% found this document useful (0 votes)
14 views6 pages

Java Class Creation and Object Access

Uploaded by

Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Java Class Creation and Object Access

Uploaded by

Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture Notes: Defining a Class in Java, Adding Variables

and Methods, Creating Objects, and Accessing Class


Members

1. Defining a Class in Java


A class in Java is a blueprint for creating objects. It defines properties (variables) and behaviors
(methods) that the objects created from the class will have.

Syntax for Defining a Class

class ClassName {
// Variables (fields)
// Methods
}

class: Keyword to declare a class.


ClassName: Name of the class, following Java naming conventions (e.g., PascalCase).
The class body is enclosed in curly braces {} .

Example

class Car {
// Class body
}

2. Adding Variables (Fields)


Variables defined inside a class are called fields. They represent the state or properties of objects
created from the class.

Syntax for Declaring Fields

accessModifier dataType variableName;


accessModifier: Controls visibility (e.g., public , private , protected ).
dataType: Type of data (e.g., int , String , double ).
variableName: Name of the variable (e.g., camelCase).

Example

class Car {
// Fields
public String brand;
private int speed;
private double price;
}

brand : A public field to store the car's brand.


speed : A private field to store the car's speed.
price : A private field to store the car's price.

3. Adding Methods
Methods define the behavior of the class. They can manipulate fields or perform actions.

Syntax for Declaring Methods

accessModifier returnType methodName(parameters) {


// Method body
}

returnType: Data type of the value returned (use void if nothing is returned).
methodName: Name of the method (e.g., camelCase).
parameters: Input values (optional).

Example

class Car {
public String brand;
private int speed;
private double price;
// Method to set speed
public void setSpeed(int newSpeed) {
if (newSpeed >= 0) {
speed = newSpeed;
}
}

// Method to get speed


public int getSpeed() {
return speed;
}

// Method to display car details


public void displayDetails() {
System.out.println("Brand: " + brand + ", Speed: " + speed + ", Price: $" + pric
e);
}
}

setSpeed : Sets the value of the private speed field.


getSpeed : Returns the value of the private speed field.
displayDetails : Prints the car's details.

4. Creating Objects
Objects are instances of a class. You create an object using the new keyword.

Syntax for Creating an Object

ClassName objectName = new ClassName();

Example

public class Main {


public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car();
}
}
myCar is an object of the Car class.
The new keyword allocates memory for the object and calls the class's constructor.

5. Accessing Class Members


Class members (fields and methods) are accessed using the dot operator ( . ).

Accessing Fields

objectName.fieldName; // To read or modify

Calling Methods

objectName.methodName(arguments);

Example

public class Main {


public static void main(String[] args) {
// Creating an object
Car myCar = new Car();

// Accessing fields
myCar.brand = "Toyota"; // Setting the public field

// Calling methods
myCar.setSpeed(120); // Setting speed using a method
myCar.displayDetails(); // Calling method to display details
}
}

Output
Brand: Toyota, Speed: 120, Price: $0.0

myCar.brand = "Toyota" : Sets the brand field.


myCar.setSpeed(120) : Calls the setSpeed method to set the speed field.
myCar.displayDetails() : Calls the displayDetails method to print the car's details.

6. Complete Example
Below is a complete program demonstrating a class, fields, methods, object creation, and member
access.

class Car {
// Fields
public String brand;
private int speed;
private double price;

// Constructor
public Car(String brand, int speed, double price) {
this.brand = brand;
this.speed = speed;
this.price = price;
}

// Methods
public void setSpeed(int newSpeed) {
if (newSpeed >= 0) {
speed = newSpeed;
}
}

public int getSpeed() {


return speed;
}

public void displayDetails() {


System.out.println("Brand: " + brand + ", Speed: " + speed + ", Price: $" + pric
e);
}
}
public class Main {
public static void main(String[] args) {
// Creating objects
Car car1 = new Car("Toyota", 120, 25000.0);
Car car2 = new Car("Honda", 100, 22000.0);

// Accessing fields and methods


car1.displayDetails();
car2.setSpeed(150);
car2.displayDetails();
}
}

Output

Brand: Toyota, Speed: 120, Price: $25000.0


Brand: Honda, Speed: 150, Price: $22000.0

Key Points
Encapsulation: Use private fields and public methods (getters/setters) to control access to
data.
Constructors: Special methods used to initialize objects (e.g., Car(String brand, int speed,
double price) ).
Access Modifiers:

public : Accessible from everywhere.


private : Accessible only within the class.

this Keyword: Refers to the current object, used to distinguish between instance variables and
parameters.

You might also like