Oop in java (post 3)

  • Creating Class

Class definition:

Assessor modifier Class CLas_Name







you can set up variables outside of methods that all the methods in          your class  can see. These are known as Field variables (or Instance variables). You set them up in exactly the same way as any other variable.




java constructor has the same name as the name of the class to which it belongs. Constructor’s syntax does not include a return type, since constructors never return a value.

Constructors may include parameters of various types. When the constructor is invoked using the new operator, the types must match those that are specified in the constructor definition.

Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided.

The only action taken by the implicit default constructor is to call the superclass constructor using the super() call. Constructor arguments provide you with a way to provide parameters for the initialization of an object.

Below is an example of a cube class containing 2 constructors. (one default and one parameterized constructor).

public class Cube1 { 

int length;

int breadth;

int height;

public int getVolume() {

return (length * breadth * height);


Cube1() {

length = 10;

breadth = 10;

height = 10;


Cube1(int l, int b, int h) {

length = l;

breadth = b;

height = h;


public static void main(String[] args) {

Cube1 cubeObj1, cubeObj2;

cubeObj1 = new Cube1();

cubeObj2 = new Cube1(10, 20, 30);

System.out.println(“Volume of Cube1 is : ” + cubeObj1.getVolume());

System.out.println(“Volume of Cube1 is : ” + cubeObj2.getVolume());




We clearly explain methods above.


  • Types of Statement

Assignment Statement:

An assignment statement in Java uses the assignment operator (=) to assign the result of an expression to a variable. In its simplest form, you code it like this:

variable = expression;

For example:

int a = (b * c) / 4;

compound assignment operator is an operator that performs a calculation and an assignment at the same time. All Java binary arithmetic operators (that is, the ones that work on two operands) have equivalent compound assignment operators:



+= Addition and assignment
-= Subtraction and assignment
*= Multiplication and assignment
/= Division and assignment
%= Remainder and assignment

For example, the statement

a += 10;

is equivalent to

a = a + 10;

Condional Statement:

The && and || operators perform Conditional-AND and Conditional-OR operations on two Boolean expressions. These operators exhibit “short-circuiting” behavior, which means that the second operand is evaluated only if needed.

&& Conditional-AND
|| Conditional-OR


  • Types of Methods

Accessor methods:

An accessor method is used to return the value of a private field. It follows a naming scheme prefixing the word “get” to the start of the method name. For example let’s add accessor methods for firstname, middleNames and lastname:

   //Accessor for firstName
   public String getFirstName()
     return firstName;


Mutator methods:

A mutator method is used to set a value of a private field. It follows a naming scheme prefixing the word “set” to the start of the method name. For example, let’s add mutator fields for address and username:

   //Mutator for address
   public void setAddress(String address)
     this.address = address;


  • Types of Variables

Local variables:

local variable in Java is a variable that’s declared within the body of a method. Then you can use the variable only within that method. Other methods in the class aren’t even aware that the variable exists.

Here’s a program that uses a local variable:

public class HelloApp
    public static void main(String[] args)
        String helloMessage;
        helloMessage = "Hello, World!";

You don’t specify static on a declaration for a local variable. If you do, the compiler generates an error message and refuses to compile your program.


Field variable:

Variable that are declared as a member of a class. OR Variables declared outside any method/constructor but inside the class block. We discuss this above in detail.



A parameter is a variable that you pass in to a function. If you think back to math class, you spend a lot of time talking about f(x). In that case, “f” is the function and “x” is the parameter- something passed in to x that is going to change what the output of f gives you.

For example, look at the code you’ve been using in every single Java program you’ve written:

public static void main(String[] args)

That is a function that takes one argument- an array of Strings that’s called args. Then there’s also the way you write stuff to the console:
System.out.println(“Hello World”);
In that case, you have a function that takes one parameter. The value of that parameter is the string “Hello World”.

Comments are closed.