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.
- we have defined a class HelloWorld.
- public – access modifier or keyword
- static – keyword
- void – return type(returning nothing)
- main – method name called by JVM.
- String – predefined java class.
- args [] – String array , reference variable
- System – Predefined final class available in lang package.
- out – static reference variable in System class, type of PrintStream class.
- 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.
- variables
- constructors
- methods
- 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.
- instance variable
- local variable
- static variable
Instance variable.
- Instance variables defined inside the class and outside of the method.
- Scope of instance variable inside a class(only if you define instance variable as private for more details about instance variable read this post).
- 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.
- A local variable can be defined inside the method, constructor, or block.
- Scope of a local variable only inside the method, constructor, or block.
- 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.
- We define a static variable with a static keyword inside the class.
- A memory location for static variables will happen at class loading.
- 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.
- A method is set of statements which used to perform different tasks.
- 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.
- Constructor is a special type of program instruction that performs a specific task.
- Constructor name must be the same as the class name.
- Constructor doesn’t have a return type.
- 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.
- Constructor is used to creating an object with a new keyword.
- Constructor is also used to initialize the variables with some values.
- 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.

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.
- Static block and instance block in java? What are the uses of them?
- this and super in java? What is the use of these?
- Constructor chaining in java with example.
- Static variable, method, and block in java.
- instance variable in java.
- varargs in java.
- Enum in java.
- final variable, final method and final class.
- Difference between JDK, JRE, JVM and JIT in java.
Summary – We have seen Basic Core Java Examples.