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.
What to do | How to do it |
Create a new class called MyNewClass. |
|
Create a new attribute (instance variable) called var1 of type |
|
Create a Constructor for your |
|
Create a new method calleddoSomeThing in your class which does not return a value and has no parameters. |
|
Create a new method calleddoSomeThing2 in your class which does not return a value and has two parameters, an |
|
Create a new method calleddoSomeThing3 in your class which returns an |
|
Create a class called MyOtherClass with two instance variables. One will store a |
|
11.2. Working with local variable
A local variable must always be declared in a method.
What to do | How to do it |
---|---|
Declare a (local) variable of type |
|
Declare a (local) variable of type |
|
Declare a (local) variable of type |
|
Declare a (local) variable of type |
|
Declare an array of type |
|
Declare an array of type |
|
Assign 5 to the |
|
Assign the existing variable |
|
Declare an |
|
Create a new |
|
Declare an |
|
Comments
Post a Comment