Basic Core Java Examples

In this post, we will Basic core Java examples. 

Java Hello World program with an explanation.

class HelloWorld {
   
    public static void main(String args[]) {

        System.out.println("Hello, World");

    }

}

Explanation.

  1. we have defined a class HelloWorld.
  2. public – access modifier or  keyword
  3. static – keyword
  4. void – return type(returning nothing)
  5. main – method name called by JVM.
  6. String – predefined java class.
  7. args [] – String array , reference variable
  8. System – Predefined final class available in lang package.
  9. out – static reference variable in System class, type of PrintStream class.
  10. println (String args[]) – is a method defined in PrintStream class.

Class in Java. Creating an object of the class.

Class is a programming code template that is used to create an object. A class can contain.

  1. variables
  2. constructors
  3. methods
  4. blocks
public class SimpleClass {

	// variable
	int a = 10;

	// constructor
	SimpleClass() {
		System.out.println("this is constructor");
	}

	// method
	public void m1() {
		System.out.println("m1 method");
	}

	// instance initializer block
	{
		System.out.println("block");
	}

	// static block
	static {
		System.out.println("static block");
	}

	public static void main(String[] args) {

		SimpleClass simpleClass = new SimpleClass();
		simpleClass.m1();
	}
}

Understanding Variable in Java.

Variable in Java is a memory location that is used to store some value. Variable must have some specific type. It can be a primitive type or class type.

  1. instance variable
  2. local variable
  3. static variable

Instance variable.

  1. Instance variables defined inside the class and outside of the method.
  2. Scope of instance variable inside a class(only if you define instance variable as private for more details about instance variable read this post).
  3. memory allocation for instance variable will happen at object creation.

We can access the instance variable with a private access modifier inside the class only.


package variablesinjava;

public class InstanceVariableExample {

	private final String s1 = "ram";// instance variable of type String
	private final int a = 10;// instance variable of type int

	public static void main(final String[] args) {
		final InstanceVariableExample example = new InstanceVariableExample();
		// we cann't access instance variable directly inside static
		// context,need to create object
		System.out.println(example.s1);
		System.out.println(example.a);

	}
}

Local variable.

  1. A local variable can be defined inside the method, constructor, or block.
  2. Scope of a local variable only inside the method, constructor, or block.
  3. we can define a local variable as final only.

Example of a local variable.

package variablesinjava;

public class LocalVariableExample {

	// local variable inside method
	public void m1() {
		final int a = 10;
		System.out.println("local variable inside method  " + a);
	}

	// local variable inside block
	{
		final int a = 20;
		System.out.println("local variable inside block  " + a);
	}

	// local variable inside contructor
	LocalVariableExample() {
		final int a = 30;
		System.out.println("local variable inside constructor  " + a);
	}

	public static void main(final String[] args) {

		final LocalVariableExample example = new LocalVariableExample();
		example.m1();

	}
}

Static variable.

  1. We define a static variable with a static keyword inside the class.
  2. A memory location for static variables will happen at class loading.
  3. The scope of the static variable is for the whole application.
package variablesinjava;

public class StaticVariableExample {

	static int a = 10;
	public static void main(final String[] args) {
		System.out.println(a);
	}
}

Understanding method in Java.

  1. A method is set of statements which used to perform different tasks.
  2. we can call the method with object/this/super/class name.
package variablesinjava;

public class StaticVariableExample {

	public void m1() {
		int a = 10;
		int b = 20;
		System.out.println("user defined method for addition of two number "
				+ (a + b));
	}

	public static void main(final String[] args) {
		StaticVariableExample example = new StaticVariableExample();
		example.m1();
		System.out.println("main method");
	}
}

Understanding Constructor in java with Example.

  1. Constructor is a special type of program instruction that performs a specific task.
  2. Constructor name must be the same as the class name.
  3. Constructor doesn’t have a return type.
  4. We can’t declare constructor as static, final, or abstract.

Types of the constructor.


1. Default constructor
2. Parameterized Constructor

Default constructor Example.


package constructorexample;

public class ConstructorExample1 {
	// default constructor, It will get invoked at time of object creation.
	ConstructorExample1() {
		System.out.println("default constructor");
	}

	public static void main(final String[] args) {
		ConstructorExample1 example1 = new ConstructorExample1();
	}

}

Output of above program is – default constructor

Parameterized Constructor  Example.

package constructorexample;

public class ConstructorExample1 {
	// Parameterized constructor, It will get invoked at time of object creation.
	ConstructorExample1(final String name) {

		System.out.println("parametrized constructor value of name is " + name);
	}

	public static void main(final String[] args) {

		ConstructorExample1 example1 = new ConstructorExample1("ram");
	}

}

Output is – parametrized constructor value of name is ram

Use of Constructor.

  1. Constructor is used to creating an object with a new keyword.
  2. Constructor is also used to initialize the variables with some values.
  3. Constructor will be invoked at the time of object creation.
package constructorexample;

public class ConstructorExample1 {
	// This constructor will get invoked during object creation.
	ConstructorExample1() {
		System.out.println("constructor invoked");
	}

	public static void main(final String[] args) {
		ConstructorExample1 example1 = new ConstructorExample1();
	}

}

Output is – constructor invoked

Let’s see an example of the initialization of variables with some values using the constructor.

package constructorexample;

public class ConstructorExample1 {

	ConstructorExample1(final String name) {
		System.out.println("value of name is  " + name);
	}

	public static void main(final String[] args) {
		ConstructorExample1 example1 = new ConstructorExample1("ram");
	}

}

Defining Contractors as Private.

Yes we can make constructor private. But we can create object inside that class only.

Basic Core Java Examples
package constructorexample;

class Hello{
	private Hello(){
		
	}
	
	public void m1(){
		//we can create object of Hello class inside Hello class only
		//because constructor is private
		Hello h1 = new Hello();
	}
}

public class ConstructorWithPrivateAccessModifier {
public static void main(String[] args) {
	//below line of code will give compilation error
	Hello h1 = new Hello();
}
}

That’s all about Basic Core Java Examples.

Other Basic Core Java Tutorials.


  1. Static block and instance block in java? What are the uses of them?
  2. this and super in java? What is the use of these?
  3. Constructor chaining in java with example.
  4. Static variable, method, and block in java.
  5. instance variable in java.
  6. varargs in java.
  7. Enum in java.
  8. final variable, final method and final class.
  9. Difference between JDK, JRE, JVM and JIT in java.

Summary – We have seen Basic Core Java Examples.