- Class Declaration:
- In Java, a class is declared using the
class
keyword followed by the class name. It serves as a blueprint for creating objects. - Example:
public class MyClass { }
- In Java, a class is declared using the
- Fields (Instance Variables):
- Fields, also known as instance variables, are the attributes or properties of a class.
- They represent the state of objects and hold data specific to each object created from the class.
- Example:
public int age;
- Constructors:
- Constructors are special methods used for initializing objects.
- They have the same name as the class and are called automatically when an object is created.
- Constructors can be parameterized or non-parameterized.
- Example:
public MyClass() { }
- Methods:
- Methods are functions defined within a class that define the behavior of objects.
- They perform operations and manipulate the data of objects.
- Methods can have parameters and return values.
- Example:
public void displayInfo() { }
- Object Creation and Usage:
- Objects are instances of a class created using the
new
keyword followed by the class name and optional arguments to constructors. - Once created, objects can access fields and methods of the class.
- Example:
MyClass obj = new MyClass();
- Objects are instances of a class created using the
In summary, class declaration defines the structure of a class, fields hold the state of objects, constructors initialize objects, methods define the behavior of objects, and object creation allows us to instantiate and use objects based on the class blueprint. These aspects form the foundation of object-oriented programming in Java.
Example programme -Run on your IDE
public class studentApp {
public static void main(String[] args) {
Student student1=new Student(“Pekka”,”123456″);
Student student2=new Student(“Tommi “,”123457”,10,true);
student1.bePresent();
student1.addStudyrecord(15);
student1.addStudyrecord(25);
student2.addStudyrecord(5);
student1.printInfo();
student2.printInfo();
}
}
class Student{
public String name;
public String studentNumber;
public int credits;
public boolean attendentStatus;
public Student(){
name=” “;
studentNumber=” “;
credits=0;
attendentStatus=false;
}
public Student(String name,String studentNumber ){
this.name=name;
this.studentNumber=studentNumber;
credits=0;
attendentStatus=false;
}
public Student(String name,String studentNumber,int credits,boolean attendentStatus ){
this.name=name;
this.studentNumber=studentNumber;
this.credits=credits;
this.attendentStatus=attendentStatus;
}
public void addStudyrecord(int creditAmount)
{
if (attendentStatus) {
credits=credits+creditAmount;}
else
{
System.out.println(“Student is absent cant add records”);
}
}
public void printCreditInfo()
{
System.out.println(“Has “+ credits+” Credits”);
}
public void bePresent()
{
attendentStatus=true;
}
public void beAbsent()
{
attendentStatus=false;
}
public void printInfo()
{
System.out.println(“name :”+ name);
System.out.println(“Student number :”+ studentNumber);
printCreditInfo();
if (attendentStatus) {
System.out.println(“Student is present”);
}
else
{
System.out.println(“Student is Absent”);
}
}
}
explanation
In the provided example, there are two classes: studentApp
and Student
. Let’s break down each part and explain how classes are used in Java:
- Class Declaration:
- The
studentApp
class is the main class that contains themain
method, serving as the entry point of the Java application. - The
Student
class defines the blueprint for creating student objects.
- The
- Fields (Instance Variables):
- In the
Student
class, there are several instance variables (name
,studentNumber
,credits
,attendentStatus
) representing the attributes of a student.
- In the
- Constructors:
- The
Student
class has three constructors: a default constructor, a parameterized constructor with name and student number, and a parameterized constructor with all attributes. - Constructors are used to initialize the state of objects when they are created.
- The
- Methods:
- The
addStudyrecord(int creditAmount)
method in theStudent
class is used to add study credits to a student’s record, but only if the student is present (attendentStatus=true
). - The
bePresent()
andbeAbsent()
methods are used to change the attendance status of a student. - The
printInfo()
method prints information about a student, including name, student number, credits, and attendance status.
- The
- Object Creation and Usage:
- In the
main
method of thestudentApp
class, twoStudent
objects (student1
andstudent2
) are created using different constructors, demonstrating constructor overloading. - Methods like
addStudyrecord
,bePresent
, andprintInfo
are called on these objects to perform actions and display information about the students.
- In the