An object has both a state and behavior. The state defines the object, and the behavior defines what the object does.
// Creating object
//Classname objectname=new Classname();
Circle object1=new Circle();
Circle object2=new Circle();
Circle object3=new Circle();
Employee emp=new Employee();
Classes are constructs that define objects of the same type. A Java class uses variables to define data fields and methods to define behaviors. Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class.
public class Employee{
}
public Employee(){
public Employee(String fname, String lname, String phone, Address address,
String SSN){
firstName = fname;
lastName = lname;
phoneNumber = phone;
this.address = address;
this.SSN=SSN;
}
}
A class may be defined without constructors. In this case, a no-arg constructor with an empty body is implicitly declared in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly defined in the class.
class Car{
public String model;
public String color;
public int seats;
public Car(){
System.out.println("In Constructor");
model="test";
color="red";
seats=4;
}
public void display(){
System.out.println("Model" + model);
System.out.println("Color" + color);
System.out.println("Seats" + seats);
}
}
public class CarExample{
public static void main (String [] args){
//Do we get error here?
Car car=new Car();
car.display();
Car ferrari= new Car("Ferrari F130","Red",2)
ferrari.display();
}
}
new ClassName();
Example:
Circle c1= new Circle();
Circle c2 new Circle(5.0);
To reference an object, assign the object to a reference variable
To declare a reference variable, use the syntax:
ClassName objectRefVar;
Example:
Circle myCircle;
Classname objectRefVar=new Classname();
Example;
Circel mycircle=new Circle();
Referencing the object’s data:
objectRefVar.data
e.g., myCircle.radius
arrayName.length
// Invoking the object’s method:
//objectRefVar.methodName(arguments)
e.g., myCircle.getArea()
stringName.length()
Public Employee(){
Private string firstName
Public Employee(string fname)
{
firstName=fname
}
}
Public Employee(){
printEmployeeRecord(){
}
}
Public Employee(){
public void setFirstName(String value) {
firstName = value;
}
public int getDeptID() {
return deptID;
}
}
A variable or method that is shared by all instances of a class is called a class variable or class method. You recognize such a variable in Java by the static keyword in the declaration. A static variable will instantiate only one copy of the variable for the whole class instead of a separate copy for each instance of a class. A static variable belongs to a class, not to an instance of the class.
Static variables/methods can NOT access non-static variables/methods!
Static methods can serve as utility methods where you don’t have to declare an object of the class to use the method and all dependencies for the static method can be passed in from the class.
Public Employee(){
public double getSalary() {
return salary;
}
public int getDeptID() {
return deptID;
}
}
Java provides a system-independent encapsulation of date and time in the java.util.Date class. You can use the Date class to create an instance for the current date and time and use its toString method to return the date and time as a string.
java.util.Date date = new java.util.Date();
System.out.println(date.toString());
// displays a string like Sun Mar 09 13:50:19 EST 2023.
The data fields can be of reference types. For example, the following Student class contains a data field name of the String type.
public class Student {
String name; // name has default value null
int age; // age has default value 0
boolean isScienceMajor; // isScienceMajor has default value false
char gender; // c has default value '\u0000'
}
To protect data.
To make class easy to maintain.
public class StaticPerson {
private static String firstName="Shiva";
private static String lastName="Sharma";
public StaticPerson(String fname, String lname) {
fname = firstName;
lastName = lname;
}
public static String getName() {
return firstName + " " + lastName;
}
//setter: Mutator
public static void setFirstName(String value) {
firstName = value;
}
static{
System.out.println("static block of parent class");
}
}
For a class to be immutable, it must mark all data fields private and provide no mutator methods and no accessor methods that would return a reference to a mutable data field object.
The scope of instance and static variables is the entire class. They can be declared anywhere inside a class.
The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be initialized explicitly before it can be used.
The this keyword is the name of a reference that refers to an object itself. One common use of the this keyword is reference a class’s hidden data fields.
Another common use of the this keyword to enable a constructor to invoke another constructor of the same class
class Car{
public String model;
public String color;
public int seats;
public Car(String model,String color,int seats){
model=model;
color=color;
seats=seats;
}
public Car(String model,String color,int seats){
this.model=model;
this,color=color;
this.seats=seats;
}
public void startEngine(){
System.out.println("Engine has started");
}
public void drive(){
//what will happen if we remove this keyword?
this.startEngine();
System.out.println("Car is moving");
}
public void display(){
System.out.println("Model" + model);
System.out.println("Color" + color);
System.out.println("Seats" + seats);
}
}
public class Circle {
private double radius;
public Circle(double radius) {
this.radius=radius;
//this must be explicitly used to reference the
// of the object being constructed data field radius
}
public Circle() {
this(1.0)
//this is used to invoke another constructor
}
public double getArea(){
return this.radius * this.radius * Math.PI;
//Every reference variable belongs to an
//instance represented by this.
}
}