Class
✅ Why do we need class?
- collect each student’s charecteristics in one class
- create one student class
and save one student’s name, age, grade in one class
- there is
int type,String typein Java - we save numbers in
int typeand letters, words inString type - why not create
Student typewhen we need, and save students inStudent typeclass?
👎🏻 before
- student1, student2 contents are distributed into arrays
- more difficult to know about student1
1
2
3
String[] studentNames = {"학생1, 학생2"}
int[] studentAges = {15, 16};
int[] studentGrades = {90, 80};
👍🏻 after
- create student class
1
2
3
4
5
6
public class Student {
String name;
int age;
int grage;
}
- create student in Main class
- save one student fields in one class
1
2
3
4
5
6
7
8
public class studentMain {
public static void main(String[] args) {
Student student1 = new Student(); //create student
student1.name = "student1"; //save student fields
student1.age = 15;
student1.grage = 90;
}
}
✔️ class is like a blueprint
- create
typesthat developer needs - create
studentblueprint - and save student1, student2…
✅ Class, Object, Instance
student class: classstudent1,student2… : instancestudent1andstudent2are different objectsstudent1object is an instance ofstudentclass- class is a blueprint of an instance
- defines field and method of instance
- instance is created based on class
- object and instance are used interchangeably
This post is licensed under CC BY 4.0 by the author.