CodeGym /Java Course /Java Syntax /Classy classes

Classy classes

Java Syntax
Level 5 , Lesson 1
Available

"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
Cat cat = new Cat();
Reader reader = new BufferedReader(new InputStreamReader(System.in));
InputStream is = new FileInputStream(path);

"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:
Cat cat1 = new Cat();
cat1.name =  "Oscar";

Cat cat2 = new Cat();
cat2.name = "Smudge";

System.out.println(cat1.name);
System.out.println(cat2.name);
Oscar
Smudge
Comments (30)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Thomas Level 13, Scottsdale, United States
17 November 2021
This is my 2nd pass thru Java on Code Gym since 2018. It's so much more fun this time. How one structures and thinks about program architecture is very well explained here ! And helps me improve my understanding in other languages too.
anupam jha Level 7, India
20 December 2020
Cat cat1 = new Cat(); here what is cat1 ? i thought name but its not the name.what is this? reference variable? clear this confusion.why do u need this in having an object? Thanks.
Random9 Level 14, Cicero, United States
23 December 2020
So yes, cat1 is the name of the Cat object. However, if you notice, the cat object created has a variable inside it called name. So when calling cat1.name, the program will return the value of the variable name inside of cat1, not the actual name of the object cat1. I hope that makes sense.
anupam jha Level 7, India
24 December 2020
u mean cat 1 is nt the 'actual name'? i guess cat1 is jst for reference to the object cat.something refering to the object.we use it to get instance variable values or use it to tell the object to do tasks(methods). anywway thanks.Keep learning...keep sharing....
Mihai Bone Level 8, Bucharest, Romania
6 October 2020
Take a deep breath and click Next lesson >>>
ViperWolfAlpha Level 5, Los Angeles, United States
1 October 2020
Nice intro to classes.
Ravi Level 8, Chennai, India
16 May 2020
Maryem Vickers Level 7, HT..., United Kingdom
13 August 2020
Uhm Maybe ask that woman who works with codegym can't remember her name.
Maryem Vickers Level 7, HT..., United Kingdom
25 August 2020
Yes Regina.
Will Level 8, Orlando, United States
14 April 2020
Please note, atoms make up elements. Atoms are not elements. Hydrogen, Oxygen, Iron, Uranium are elements.
M Till Level 7, San Jose, United States
26 July 2020
I know an atom is an atom and an element is an element, but the element hydrogen is a hydrogen atom and visa-versa, no?
Russell Level 9, Galmpton, Brixham , United Kingdom
13 August 2020
Well, thats not strictly correct is it. I mean, an element in definition, is a specific structure of Protons, Neutrons and Electrons. In the same way, an atom is an arrangement of Protons, Neutrons and Electrons in one structure. For example. the element Helium, refers to a structure of 2 Protons, 2 Neutrons and 2 electrons. We refer to this as an atom of helium. But its interchangeable. An atom is made up of specific structures that describe its element status. Each Atom is by definition an elemental object. Molecules use more than one atom(element) Elements are single atomic structures. So therefore, atoms are elements and elements are atoms. We do however refer to a collection of the *same-structured* atoms as an element as well. Which is why it can cause confusion. So; An atom is arranged in a specific way that we describe as an Element. And an Element can only consist of the same-structured atoms. You wouldnt call brass an element because it is a mixture of copper and zinc. 2 different elements, and 2 different *atom types* Yes, Isotopes are differently structured elemental atoms, but those atoms still follow an atomic structure. Look at it in a programming way.: Class Helium3 : public Helium
Maryem Vickers Level 7, HT..., United Kingdom
13 August 2020
XD I was thinking that to XD
Maryem Vickers Level 7, HT..., United Kingdom
13 August 2020
*too
Sergo Level 7, Kroke, Poland
10 November 2019
Go ahead, man!!! Don't be so lazy! :)
Cristian Level 16, Bucharest, Romania
6 September 2019
Fain!
Gabriella LaPlace Level 16, Basseterre, Saint Kitts and Nevis
30 July 2019
Kool.
Dinesh Level 7, Delhi, India
27 January 2021
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." Can You explain this with example Thank You.
jaiveer chand Level 7, Mumbai, India
14 June 2019
I am starting to feel like iron man who is preparing to make MARK 1