Java Classes For Day 12 Of Javatober

in #java6 years ago

Taking A More In Depth Look At Java Classes For Day 12

code-coding-computer-879109.jpg
Image courtesy of pexels.com

Getting through the CodeGym lessons to catch up with my knowledge I have accrued in my Month of Java. We started Object Oriented Programming yesterday and we are extending things further today with a more specific look at Java Classes.

Java Classes


You could say that classes form the cornerstone of Java programming. When you become a programmer, nearly your entire job will be writing your own classes that have various functions. In our previous post we discussed how Java is an Object Orientated Programming language where all programs consist of objects that in one way or another are related to each other. We also mentioned that a class is a template for an object which determines what the object will look like and what functions it will have.

public class Cats {
    String catName; // fields
    int catAge;
}

The variables in the class are called fields, so as we did last time, we created a Cat object with the two variables. Each instance of the Class will have these variables which is why we call them instance variables. As well as instance variables, we can also create class variables, or static variables as they will be defined with the word static, which means it belongs to the class and not the object.

As well as variables, a class can also have methods, which defines your classes functionality. The main part of the class is a method, but as it has the word static, it belongs to the class and not each object. A non-static method can only be called on a specific object we've created.

Constructors create a template in our code to ensure our fields are specified when we create our objects

    // Constructor for the Cat class
    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

In the constructor above, it means that we need to include the name and age in our object when it is created. Java will create its own constructor if you do not define it but it will not specify that specific fields are needed. The constructor lets the compiler know what objects should look like when they are created, so they are not created without specific information needed.

Code For The Day

  1 public class Cat {
  2 
  3     String name; // fields or instance variables
  4     int age;
  5 
  6     static int count = 0;  // static class variables
  7 
  8     public Cat() {  // method 
  9         count++;
 10         this.name = "Street cat No. " + count;
 11     }
 12 
 13     public Cat(String name, int age) { // constructor for the class
 14         this.name = name;
 15         this.age = age;
 16     }
 17 
 18     public static void main(String[] args) { // main method of the class
 19 
 20         Cat streetCat1 = new Cat();
 21         Cat streetCat2 = new Cat();
 22         System.out.println(streetCat1.name);
 23         System.out.println(streetCat2.name);
 24     }
 25 }

I'll be posting daily, sharing my experiences on my “1 Month of Java Code” experiences”, my previous post on day 11 can be found below, so feel free to have a look:
https://steemit.com/java/@run.vince.run/objects-in-day-11-of-one-month-of-java

If you have found this post useful or interesting, please consider Commenting, Upvoting, Following and/or Resteeming

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by run.vince.run from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.033
BTC 63630.77
ETH 3179.32
USDT 1.00
SBD 3.95