package lesson91;
public class ClassAndObject {
public static void main(String[] args) {
Student Mark = new Student(); // Mark-->object/instance
Mark.setName("Mark");
Mark.setAge(20);
Mark.setID(1);
Mark.setGender("Male");
System.out.println(Mark.getID() + " " + Mark.getName() + " " + Mark.getAge() + " " + Mark.getGender());
}
}
package lesson91;
public class Student {
public static final String getAge = null;
int ID;
int age;
String name;
String gender;
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public static String getGetage() {
return getAge;
}
//encapsulation
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
A class–the basic building block of an object-oriented language such as Java–is a template that describes the data and behavior associated with instances of that class. When you instantiate a class you create an object that looks and feels like other instances of the same class. The data associated with a class or object is stored in variables; the behavior associated with a class or object is implemented with methods. Methods are similar to the functions or procedures in procedural languages such as C.
(From:Defining a Class)
Class Format:
Modifiers --> public
Class
Class name -->Student
Two braces --> {}
We also have extend and implements, we will talk about it later(after learning inheritance).
Don’t forget to encapsulate your code by using Generate Getters and Setters.
After we defined a class we can create an object by using following instruction:
Declaration --> Student mark =
Instantiation --> new
Initialization --> Student();