Java - tutorial - 09/13 - classes

revision:


Java - classes

OOP (= Object-Oriented Programming) is about creating objects that contain both data and methods. OOP has several advantages: is faster and easier to execute; provides a clear structure for the programs; helps to keep the Java code DRY (= "Don't Repeat Yourself"), and makes the code easier to maintain, modify and debug; makes it possible to create full reusable applications with less code and shorter development time.

Classes and objects are the two main aspects of object-oriented programming. A class is a template for objects, and an object is an instance of a class. When the individual objects are created, they inherit all the variables and methods from the class.

Everything in Java is associated with classes and objects, along with its attributes and methods. A Class is like an object constructor, or a "blueprint" for creating objects.

To create a class, use the keyword "class": a class should always start with an uppercase first letter, and the name of the java file should match the class name.

example:

        public class Main {
            int x = 5;
          }
    

In Java, an object is created from a class: to create an object of "Main", specify the class name, followed by the object name, and use the keyword "new".

example:

        public class Main {
            int x = 5;
            public static void main(String[] args) {
                Main myObj = new Main();
                System.out.println(myObj.x); // 5
            }
        }
    

multiple objects of one class can be created.

example:

        public class Main {
            int x = 5;
          
            public static void main(String[] args) {
              Main myObj1 = new Main();
              Main myObj2 = new Main();
              System.out.println(myObj1.x); // 5
              System.out.println(myObj2.x); // 5
            }
          }
    

using multiple classes: you can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)). Remember that the name of the java file should match the class name.


class attributes

class attributes are variables within a class. Another term for class attributes is fields.

example: create a class with attributes x and y

        public class Main {
            int x = 5;
            int y = 3;
          }
    

attributes can be accessed by creating an object of the class, and by using the dot syntax (.).

example: create a class with attributes x and y

        public class Main {
            int x = 5;
          
            public static void main(String[] args) {
              Main myObj = new Main();
              System.out.println(myObj.x); // 5 
            }
          }
    

attribute values an also be modified or overridden. If you don't want the ability to override existing values, declare the attribute as final. The "final keyword" is useful when you want a variable to always store the same value, like PI (3.14159...). The final keyword is called a "modifier".

example:

        public class Main {
            int x;
          
            public static void main(String[] args) {
              Main myObj = new Main();
              myObj.x = 40;
              System.out.println(myObj.x); // 40
            }
        }
    

example:

        public class Main {
            final int x = 10;
          
            public static void main(String[] args) {
              Main myObj = new Main();
              myObj.x = 25; // will generate an error
              System.out.println(myObj.x); 
            }
        }
    

If you create multiple objects of one class, you can change the attribute values in one object, without affecting the attribute values in the other. Moreover, you can specify as many attributes as you want.

You also can specify as many attributes as you want.