DEV Community

Cover image for 7. Variables and methods
Raghav Khanna
Raghav Khanna

Posted on

7. Variables and methods

7.1. Variable
Variables allow the Java program to store values during the runtime of the program.

A variable can either be a: * primitive variable * reference variable

A primitive variable contains the value. A reference variable contains a reference (pointer) to the object. Hence, if you compare two reference variables, you compare if both point to the same object. To identify if objects contain the same data, use the object1.equals(object2) method call.

7.2. Instance variable
Instance variable is associated with an instance of the class (also called object). Access works over these objects.

Instance variables can have any access control and can be marked final or transient. Instance variables marked as final cannot be changed after a value has been assigned to them.

7.3. Local variable
Local (stack) variable declarations cannot have access modifiers. Local variables do not get default values, so they must be initialized before they can be used.

final is the only modifier available to local variables. This modifier defines that the variable cannot be changed after the first assignment.

7.4. Methods
A method is a block of code with parameters and a return value. It can be called on the object.

package com.vogella.javaintro.base;

public class MyMethodExample {
void tester(String s) {
System.out.println("Hello World");
}
}
Methods can be declared with var-args. In this case the method declares a parameter which accepts everything from zero to many arguments (syntax: type …​ name;) A method can only have one var-args parameter and this must be the last parameter in the method.

Overwrite of a superclass method: A method must be of the exact same return parameter and the same arguments. Also the return parameter must be the same. Overload methods: An overloaded method is a method with the same name, but different arguments. The return type can not be used to overload a method.

7.5. Main method
A public static method with the following signature can be used to start a Java application. Such a method is typically called main method.

public static void main(String[] args) {

}
7.6. Constructor
A class contains constructors that are invoked by the Java runtime to create objects based on the class definition.

Constructor declarations look like method declarations except that they use the name of the class and have no return type.

A class can have several constructors with different parameters.

In the following example the constructor of the class expects a parameter.

package com.vogella.javaintro.base;

public class MyConstructorExample2 {

String s;

public MyConstructorExample2(String s) {
    this.s = s;
}

}
Each class must define at least one constructor. If no explicit constructor is defined in the Java source file, the compiler implicitly adds a constructor. If the class is sub-classed, then the constructor of the super class is always called implicitly in this case.

In the following example the definition of the constructor without parameters (also known as the empty constructor) is unnecessary. If not specified, the compiler would create one.

package com.vogella.javaintro.base;

public class MyConstructorExample {

// unnecessary: would be created by the compiler if left out
public MyConstructorExample() {
}

}
The naming convention for creating a constructor is the following: classname (Parameter p1, …​) { }.

Every object is created based on a constructor. This constructor method is the first statement called before anything else can be done with the object.

Top comments (1)

Collapse
 
erraghavkhanna profile image
Raghav Khanna

If anyone here Appriciate my work on JAVA series please do a comment.. So that i can continue with it..
If you want i will continue my series on JAVA.
But please let m know. 🙂