More Java language constructs

 


10. More Java language constructs

10.1. Class methods and class variables

Class methods and class variables are associated with the class and not an instance of the class, i.e., objects. To refer to these elements, you can use the classname and a dot (".") followed by the class method or class variable name.

Class methods and class variables are declared with the static keyword. Class methods are also called static methods and class variables are also called static variables or static fields.

An example for the usage of a static field is println of the following statement:System.out.println("Hello World"). Hereby out is a static field, an object of type PrintStream and you call the println() method on this object.

If you define a static variable, the Java runtime environment associates one class variable for a class no matter how many instances (objects) exist. The static variable can therefore be seen as a global variable.

The following code demonstrates the usage of static fields.

package com.vogella.javaintro.base;

public class MyStaticExample {

    static String PLACEHOLDER = "TEST";

    static void test() {
        System.out.println("Hello");
    }
}
package com.vogella.javaintro.base;

public class Tester {

    public static void main(String[] args) {
        System.out.println(MyStaticExample.PLACEHOLDER);
        MyStaticExample.test();
    }

}

If a variable should be defined as constant, you declare it with thestatic and the final keyword.

The static method runs without any instance of the class, it cannot directly access non-static variables or methods.

10.2. Abstract class and methods

A class and method can be declared asabstract. An abstract class can not be directly instantiated.

If a class has at least one method, which only contains the declaration of the method, but not the implementation, then this class is abstract and can not be instantiated. Sub-classes need then to define the methods except if they are also declared as abstract.

If a class contains an abstract method, it also needs to get defined with the abstract keyword.

The following example shows an abstract class.

package com.vogella.javaintro.base;

public abstract class MyAbstractClass {
    abstract double returnDouble();
}

11. Cheat Sheets

During your Java development time, you will be asked to do certain things, like creating a local variable. The following can be used as a reference for such tasks, so that you how what you have to do.

11.1. Working with classes

While programming Java you have to create several classes, methods, instance variables. The following uses the package test.

Table 2. Cheatsheet

What to do

How to do it

Create a new class called MyNewClass.

package test;

public class MyNewClass {

}

Create a new attribute (instance variable) called var1 of type String in the MyNewClass class

package test;

public class MyNewClass {
    private String var1;
}

Create a Constructor for your MyNewClassclass which has aStringparameter and assigns the value of it to the var1instance variable.

package test;

public class MyNewClass {
    private String var1;

    public MyNewClass(String para1) {
        var1 = para1;
        // or this.var1= para1;
    }
}

Create a new method calleddoSomeThing in your class which does not return a value and has no parameters.

package test;

public class MyNewClass {
    private String var1;

    public MyNewClass(String para1) {
        var1 = para1;
        // or this.var1= para1;
    }

    public void doSomeThing() {
    }

}

Create a new method calleddoSomeThing2 in your class which does not return a value and has two parameters, an int and a Person

package test;

public class MyNewClass {
    private String var1;

    public MyNewClass(String para1) {
        var1 = para1;
        // or this.var1= para1;
    }

    public void doSomeThing() {

    }

    public void doSomeThing2(int a, Person person) {

    }

}

Create a new method calleddoSomeThing3 in your class which returns an intvalue and has three parameters, two Strings and a Person.

package test;

public class MyNewClass {
    private String var1;

    public MyNewClass(String para1) {
        var1 = para1;
        // or this.var1= para1;
    }

    public void doSomeThing() {

    }

    public void doSomeThing2(int a, Person person) {

    }

    public int doSomeThing3(String a, String b, Person person) {
        return 5; // any value will do for this example
    }

}

Create a class called MyOtherClass with two instance variables. One will store a String, the other will store a Dog. Create getter and setter for these variables.

package test;

public class MyOtherClass {
    String myvalue;
    Dog dog;

    public String getMyvalue() {
        return myvalue;
    }

    public void setMyvalue(String myvalue) {
        this.myvalue = myvalue;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
}

11.2. Working with local variable

A local variable must always be declared in a method.

Table 3. Title
What to doHow to do it

Declare a (local) variable of type String.

String variable1;

Declare a (local) variable of type String and assign "Test" to it.

String variable2 = "Test";

Declare a (local) variable of type Person

Person person;

Declare a (local) variable of type Person, create a new Object and assign the variable to this object.

Person person = new Person();

Declare an array of type String

String array[];

Declare an array of type Personand create an array for this variable which can hold 5 persons.

Person array[]= new Person[5];

Assign 5 to the int variable var1 (which was already declared);

var1 = 5;

Assign the existing variable pers2 to the existing variable pers1;

pers1 = pers2;

Declare an ArrayListvariable which can hold objects of type Person

ArrayList<Person> persons

Create a new ArrayList with objects of type Person and assign it to the existing variable persons.

persons = new ArrayList<Person>();

Declare an ArrayListvariable which can hold objects of type Person and create a new Object for it.

ArrayList<Person> persons = new ArrayList<Person>();


Comments

Popular posts from this blog

How to Hack WhatsApp Chats

Installation of Java

Integrated Development Environment