RastcodeS

Where we take good care of both your front and back-end needs

Great place to take your code to the next level

Checkout my work and get in touch



RastcodeS Logo
Go Back

Java Break - Class Structure - Java Core Part - I


Java



Written by Rastko

date - 24/Aug/18 | views - 241

Java Break - Class Structure - Java Core Part - I

     Introduction to Java class structure and it's members - fields and methods. Also, an explanation of Java comments.

     This is the first post in a series, where we'll take a look at some core Java features. Make sure to stay tuned, as more will come.

 

     Java Classes

 

     Java may be confusing to many programmers, who have never experienced OOP - Object Oriented Programming.

     Even though the OOP was exclusive to languages like Java, it has since found its way into others.

     The basis of Java programming are classes - the core of the language. Classes are blueprints for entities you want to define in your programs. Most classes, however can't be used on their own. They have to be instantiated through objects. A common definition of an object goes:

     An object is a runtime instance of a class in memory. But what does it mean?

 

     Let's try an example:

     Say you want to define a Person class, which can hold values for the person's name, age, social security number... Ok, that's a blueprint and it would look something like this:

public class Person {
   String name;
   int age;
   String socialSecurityNumber;
}

 

     For now, you can ignore the "String" and "int", we'll get to those later, but as you can see the Person class has some defined variables, which hold no values. That's ok, as we stated before, classes are just blueprints.

     Now, an object is where we put our values. Let's say you want to make a Person object, that describes your friend John. You'd make a new object of class Person and set it's fields like:

Person john = new Person();
john.name = "John";
john.age = 30;
john.socialSecurityNumber = "123456789";

 

     The actual syntax, we'll explain later, but it is evident that in this fashion we can create any number of Person objects.

     We'll come back to objects in future articles, this was just to illustrate the relationship between classes and objects.

 

 Class Fields and Methods

 

     Java classes have two main types of elements, or to use tech term - members.

     Fields, which you have already seen in the example above are more commonly known as variables and come in many forms.

     The other member type are methods, or in other languages functions or procedures. These are often not considered synonyms, but for any of you, who are coming from other programming backgrounds, you can consider them as such.

     Here's how you may remember this easily - Fields hold values and Methods do things.

     Let's go back to our previous example of a Person class:

public class Person { 
   // Person Members

   // Name Field
   String name;

   // Age Field
   int age;

   // Social Security Number  Field
   String socialSecurityNumber;

   // sayHello Method
   public void sayHello() {
      System.out.println("Hello!");
   }
}

 

     As you can see, Person class has 4 members, 3 fields - name, age and socialSecuritynumber, and we have also includes one method - sayHello, which prints  "Hello".

     You may also be asking yourself what public and void mean. These are words with special meanings or keywords and can only be used in a specific way. We'll cover this later, but public keyword means that the Person class and the sayHello method, which are marked with it can be accessed from other classes without restrictions. The void keyword means that the method sayHello doesn't return any value. A method that returns a value will have a variable type instead of void and an obligatory return statement:

public String getName() {
   return name;
}

 

     This method returns a String, or to be more accurate the name field. The complete declaration of a method is called the method signature. We'll look at another example to complete this part:

public int increseAge(int numberOfYears) {
   return age + numberOfYears;
}

 

     Let's dissect this method. But wait, what's that within the parenthesis? That's a method parameter, it means when we call this method, we need to provide some data, in this case an integer( a whole number ). A method can require a number of parameters.

     Ok, so we have a public( it can be freely accessed from other classes ) method named increseAge , which takes an integer - number of years and also returns an integer. The part between the curly braces is called the method body and in our case just has a statement which return the sum of our Person object's age and the numberOfYears we received as a parameter. We'll discuss methods in greater details in lectures to come, but this is a good start.

 

Java Comments

 

     While other parts of our java code have many rules we have to follow, those don't really apply to comments. They don't get executed so their position doesn't really matter. Comments are mainly used to explain parts of our code to others that may be using it, or often to ourselves as we may forget what we wanted to program a year ago. They a definitely a good coding practice as they make our code easier to read. There are 3 types of comments in the Java language.

 

Single line comments:

// This comment extends until the end of the line.

    

      You've already seen these comments when we were dissecting our Person class. They start with two slashes and include a single line.

 

Multiline comments:

/* This comment
*  extends over
*  multiple lines
*/

 

     Multiple-line comments start from /* and spread until they hit the */ symbols.

 

     There is also another type of multiline comment - the Javadoc comment:

/** This is a Javadoc
* multiline comment
* @author Rastko Savic
*/

 

     As you can see, the only difference from the regular multiple-line comment is that Javadoc starts with /**. They can be used by Javadoc tool to create many useful references in our code, in the case above - the author of a certain part of a program.

 

     That's all for this part. Next, we'll take a close look at the main method, from which our program initializes. Stay tuned for the next Java Break.

 


View Post/Backend
Login to comment

Comments


No Comments yet. Be the first!

Currently working on a Laravel project

Check it out
Java Break - Declare and Initialize Variables - Java Core Part - VI
Java Break - Declare and Initialize Variables - Java Core Part - VI

     Declaring and initializing variable in Java. How to do it properly and avoid mistakes.

     The sixth chapter...

Java


Recents


Popular