Creating Your Own Class in Java

 Creating Your Own Class in Java

A class is like a template for creating objects. But what is an object in Java?

The easiest way for me to explain it, is that an object is a collection of variables and methods that are grouped together. The object's variables are called its properties and its methods are called its methods... I know right!

First we shall look at how we make a class and then we shall use the class to make an object, or an instance of that class.

Start by creating a new file called MyClass.java

In the file type this...


public class MyClass {
  
  public int myInt = 5;

} //MyClass


It might not look like much but it is a class and it is ready to be used to start spitting out objects.

Now create another file called MainClass.java

In the file type this...


public class MainClass {

  public static void main(String[] args) {


    MyClass myObject = new MyClass();
    
    System.out.println(myObject.myInt);
   

  } //main
} //MainClass


Now compile both MyClass.java and MainClass.java and then run MainClass.

We have defined a class called MyClass with a property called myInt. Then we were able to create an object called myObject, which is an instance of MyClass, and output the value of its property.

Now this is interesting. We were able to use MyClass without having to import it first. But why? The answer is because both files are in the same folder. If you create a new folder and drop MyClass into it, you will get an error when you try to run MainClass.

Exception in thread "main" java.lang.NoClassDefFoundError: MyClass

And there are other considerations... First your new folder can not have a space in the name. And your new folder can't start with a capitol letter. Sorry, I don't make the rules here. So lets call the new folder something like "newFolder".

Then you will need to add a package description to the MyClass.java file. Like this...

package newFolder;

public class MyClass {
  
  public int myInt = 5;

} //MyClass


Next, add the import line to MainClass.java. Like this...


import newFolder.MyClass;

public class MainClass {

  public static void main(String[] args) {


    MyClass myObject = new MyClass();
    
    System.out.println(myObject.myInt);
   

  } //main
} //MainClass


Then you need to recompile your java files.

javac MainClass.java

and

javac newFolder/MyClass.java


Now when you run MainClass you get output 5.


Lets talk about MyClass's property called myInt. You can see I made it public.

Lets make it private and see what happens. Recompile MyClass and try to run MainClass again.

You got an error message didn't you. Something like...

 ...IllegalAccessError: class MainClass tried to access private...


We have just learned that MainClass can not touch MyClass's privates.

So then do we want to set the property to private? Sure we do! And we will use special methods of MyClass, called getters and setters, to access the private properties. But before we get into all that you should be aware that there are three main types of access labels; public, protected, and private, and then there is the default behavior of having no access label. Here is a chart to show you how they stack up.



Now lets talk about getter and setter methods and why to use them. First lets do an experiment. Lets create a new class file called Rectangle.java and set all the properties to public. Like this...


public class Rectangle {
  
  public int length = 5;
  public int width = 3;
  public int area = 15;

} //Rectangle

Next, modify MainClass.java like this...


public class MainClass {

  public static void main(String[] args) {


    Rectangle myRect = new Rectangle();
    
    System.out.println(myRect.area);
   

  } //main
} //MainClass

When you compile them and run the code, the output is 15. It works, so what is the problem?
Maybe there is no problem... until we do this!



public class MainClass {

  public static void main(String[] args) {


    Rectangle myRect = new Rectangle();

    myRect.length = 1;
    
    System.out.println(myRect.area);
   

  } //main
} //MainClass

Since the area of a rectangle is the length x width, you can see how a Rectangle with a  length of 1 and a width of 3 should not have an area of 15? That is just stupid. And since we are not stupid, we can fix this.
Let us set all our properties to private. Then add the getter and setter methods.


public class Rectangle {
  
  private int length = 5;
  private int width = 3;
  private int area = 15;

  //Setter methods
  public void setLength(int i) {
    this.length = i;
    this.area = this.length * this.width;
  }

   public void setWidth(int i) {
    this.width = i;
    this.area = this.length * this.width;
  }

  //Getter methods
  public int getLength() {
    return length;
  }

  public int getWidth() {
    return width;
  }


  public int getArea() {
    return area;
  }


} //Rectangle

Sure that was a little more work, but now using the setter methods we can also update the area of Rectangle when the length or width is changed.
To use our setter and getter methods we change the MainClass.java file like so...


public class MainClass {

  public static void main(String[] args) {


    Rectangle myRect = new Rectangle();

    myRect.setLength(1);
    
    System.out.println(myRect.getArea());
   

  } //main
} //MainClass

And now everything checks out fine with the output matching our new Rectangle length.

Comments

Popular Posts