Start learning now
You've leveled up!
Will I manage to learn all this? CodeGym's super specialist has an answer for this question, which is critical for any who might doubt. Quote: "Programming is, among other things, a skill just like playing the guitar, swimming, or riding a bike. People are not born cyclists". We read and get inspired.
Classes are the foundation of Java. Studying this language consists of two major tasks: learning to write your own classes and learning to use other people's classes. Today we'll start with the simplest step forward: we will learn how to write unassuming classes and, of course, how to use them to create objects. Objects are also often called instances of classes. These terms are synonyms: either way is correct.
Packages aren't just for UPS and FedEx. In Java, packages are folders where various class files are stored. A class's full name depends on the package that contains the class. For large projects, packages are a must! Without them, it's easy to get confused. But with them, everything is orderly. Use packages!
Creating classes and packages
It's all very simple. If we create a Java program, it means we create certain classes in the right way in some packages. Still, sometimes beginners get confused at this stage, not knowing what to create or where. In this lesson you'll find some links to useful articles about classes, interfaces and packages.
How to create objects
An object is a variable whose type is a class. Or an instance of a class, if you like. This means that an object is created just like any new variable, or more precisely, like any reference variable. This lesson will teach you the syntax for creating objects, and give you your first exposure to special and super useful methods known as getters and setters.
Practice creating objects
At the secret CodeGym center, you'll practice on cats and dogs all over the place. But animal-rights advocates can be calm: No animal used as an object has been harmed during this practice. We have already dealt with the theory of creating classes and objects. It's time to do some tasks, get used to the syntax, and make sure it's right.
Initializing objects. Initialize method
Initialization is basically initiation into the world of objects! Someday (in fact, very soon, if you don't quit) you'll learn what encapsulation is and why it is needed. Meanwhile, we'll take our first step toward it by learning what it means to initialize objects and why we need to do it.
Practicing initializing objects
When an object is created, you have to assign initial data to its variables. This prevents situations where you might access an object that doesn't have the internal information it needs to work properly. Let's complete some initialization tasks: we'll begin by creating valid cats, dogs, friends, and other objects.
Some people think programmers are like construction workers who erect buildings. There's something to this analogy. But today we're going to talk about a different kind of constructor. Since all objects must be initialized one way or another, special abbreviated initializers were invented to perform this process. They are called constructors.
Practice with constructors
You won't find a constructor wearing a hard hat at a construction site. It's a special method for initializing objects. It's where you write initial values for an object's fields (though not necessarily all of them). A constructor has the same name as the class. One class can have many constructors. Or just one...known as the default constructor. We'll master all this through practice.
Useful links from the Professor – 5
Once again, the venerable Professor Noodles puttered around in the archives and dug up some useful materials for CodeGym students to allow them to see the material from another angle, and deepen and expand their knowledge. We'll learn about ternary operators, getters and setters, constructors, and rookie mistakes. There certainly won't be any fluff!
Nerd break: musical interlude
We're already almost at the end of the fifth level. Very soon we'll be back to doing tasks (including difficult tasks!). But for now, it's a great time to relax! You've heard of the superstar LadyJava, right? No? Time for you to get acquainted with her megahit, which warms the hearts of all Java programmers, regardless of their level.