"Hi, Amigo!"
"Hi, Ellie!"
"Today I'm going to tell you a lot about classes in Java."
"Explanation No. 1. I'll start with an analogy. All things in the material world consist of atoms. There are various types of atoms: hydrogen, oxygen, iron, uranium… Combinations of these atoms make up different molecules, substances and objects."
"These atoms have some internal structures, such as electrons and a nucleus consisting of protons and neutrons."
"Yes, I know a bit about the structure of atoms. I'm a robot, after all!"
"The world of Java is structured in a similar way. Programs consist of various types of objects (classes). Different classes, in turn, contain different internal structures (variables and methods)."
"If we look at a program as a whole, then its constituent building blocks are objects. Classes are the types of blocks. In other words, different types of blocks are objects of different classes."
"I think I understand it."
"Explanation No. 2. When we need a new object type, we create a new class and define the way its internal objects behave."
"That sounds a bit too general. It seems clear, but you didn't say anything concrete."
"In terms of the internal structure, a class consists of methods, which do something, and variables, which are used by the methods to store data."
"So, would it be simpler to say that a class is a set of methods?"
"Almost. To be more exact, a class is a group of related methods and shared variables used by these methods to store different values."
"I see. To create a new class, we first need to write these methods…"
"Yes. And we also need to decide what variables different methods will share. We pull these variables out of the methods and put them into the class, i.e. we turn local variables into member (instance) variables."
"Basically, a class is created like this:
1. The programmer decides what other objects they need.
2. The programmer divides these objects into different types, depending on what they are to do.
3. The programmer writes a separate class for each type.
4. In a class, they declare the needed methods and variables.
5. In each method, they write commands to make the method do what they want it to do.
6. The class is ready. You may now create objects of the class."
"Cool! What an interesting pattern! I'll need to remember that."
"Memorize it. It will come in handy. The programming philosophy that calls for a program to be divided into objects is called object-oriented programming (OOP)."
"Java is a classic example of an OOP language: in Java, everything is an object."
"Studying Java consists of two big tasks: learning how to write your own classes and learning how to use other people's classes. Today we'll start with the simplest of these. We'll learn how to write the simplest classes and, of course, how to create objects of these classes. Objects are also often called 'instances' of a class. They are synonyms; both expressions are correct."
"Got it."
"To summarize, we could say that a class is a mini-program: some data and functions that use the data to do something. Classes are used to create instances of classes, also known as objects."
"To create an object, write 'new class_name()
' in the code. Here are some examples:"
Examples |
---|
|
|
|
"An object has two interesting properties:"
"First. Each object stores its own copy of instance variables. This means that if instance variables x and y were declared in a class and 10 objects of that class were created, then each object will have its own variables. Changing the variables in one object doesn't affect the variables in another object."
"Second. When creating objects, you can pass different arguments. These values are used to initialize the object. A little bit like naming a newborn. Many classes require such arguments in order to create instances (objects) of the class."
"I got it. What did you say about instance variables?"
"Each object has its own data. These are the instance variables."
Java code | Screen output: |
---|---|
|
Oscar Smudge |
GO TO FULL VERSION