#Can someone please explain OOP to me please. I am very confused about "Class"
15 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @cobalt prawn! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
a class is a blueprint for a thing
in programming we like to work with data. OOP creates a framework for working with and manipulating data.
The part I dont ubderstand about the class is that why we use static and sometimes not? Sometimes I see people say "public name(){}" and sometimes "public name{}". I am very confused about this. Could you plesse explain it to me
Sometimes they use void and sometimes they dont use voif
Void
And there is no "Public" in some cases as well.
there are two types of keywords to be aware of. access modifiers which specify where a part of a class is able to be accessed. That would be public, private no modifier etc
if you are confused why we have access modifiers I could explain that as well.
when it comes to static. this has to do with the fact that it's necessary sometimes to only reference some data that doesn't belong to an instance of a class.
Here is an example of a static field in a class
public class Student {
private int id;
private static int idCounter = 0;
private String name;
private int age;
public Student(String name, int age) {
this.id = Student.idCounter++;
this.name = name;
this.age = age;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String toString() {
return String.format("%s is %d years old and has the id %d", this.name, this.age, this.id);
}
public static void main(String[] args) {
Student student = new Student("Billy", 16);
System.out.println(student);
}
}
There are 4 things that you are confusing:
- What is the Class in OOP
- What is static in Class
- Public keyword ```java
- “public name() {}” and sometimes “public name()”
Like the language in the real life, Java language describe the life into programing. So everything in Java language relate to the real life.
-
The Class in real world is a group of people that have the same attributes and actions. So in Java, the Class is the same meaning like this, it is used to make a pattern of some object.
Ex: In real world, we have RichPeople Class, they have the same attributes like: money, car,.. and actions like: makeMoney, buyCar, …. We can describe this in Java by RichPeople Class. -
The static in real word meaning: “staying in one place without moving, or not changing for a long time”. So in Java, static means “every object that belong to Class has the same static attributes or static actions”.
Ex: The RichPeople Class have static attributes like MINIMUM_MONEY = “1m $” -
Public in real word meaning: “Every one can know public things”. So in Java, it means if a Class has public attributes or public actions, the Object of other Class can know about those. You must read more about this with the keyword: “access modifiers in Java”
-
I think you have error syntax here, I think there are “public name(){}” and “public name()”. They are two type of method (or action) in Class and Interface. Once you understand Class and Interface, you should research them. And now, you don’t need to worry about it. ```
This message has been formatted automatically. You can disable this using /preferences.