Over a million developers have joined DZone.

OCAJP 7 Tutorial: Java Basics

DZone's Guide to

OCAJP 7 Tutorial: Java Basics

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

The following contents are taken from free resources available on internet and the main aim of this tutorial is to study variables declaration based on Java 7 Programmer exam objectives.

Here we have to go to discuss about the following 4 Java Basic Concepts

  • Define the scope of variables
  • Define the structure of a Java class
  • Create executable Java applications with a main method
  • Import other Java packages to make them accessible in your code

Define the scope of variables

Type of Variables

Java has following three kinds of variables: 

  1. Local variables
  2. Instance variables
  3. Class variables

Local Variables:-

  • Local variables are declared in methods, constructors, or blocks.
  • Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
  • Access modifiers cannot be used for local variables.
  • Local variables are visible only within the declared method, constructor or block.
  • There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.


dataType varName = value;

Instance Variables:-

  • Instance variables are declared in a class, but outside a method, constructor or any block.
  • When a space is allocated for an object in the heap a slot for each instance variable value is created.
  • Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.
  • Instance variables can be declared in class level before or after use.
  • Access modifiers can be given for instance variables.
  • The instance variables are visible for all methods, constructors and block in the class.
  • Instance variables have default values. For numbers the default value is 0, for Boolean it is false and for object references it is null.
  • Instance variables can be accessed directly by calling the variable name inside the class.

Syntax of Instance Variables

dataType varName1,...... varNameN;

Class Variables:-

  • Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
  • Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final and static. Constant variables never change from their initial value.
  • Static variables are created when the program starts and destroyed when the program stops.
  • Default values are same as instance variables. For numbers the default value is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or within the constructor. Additionally values can be assigned in special static initializer blocks.
  • Static variables can be accessed by calling with the class name . ClassName.VariableName.
  • When declaring class variables as public static final, then variables names are all in upper case. If the static variables are not public and final the naming syntax is the same as instance and local variables.

Syntax of Class Variables:-

class ClassName {
  dataType Varname;

Define the structure of a Java class

  • Every Java program consists of a collection of classes.
  • A class is a template for creating a particular form of object.
  • The objects created by a particular class template are called the instances or objects of that class.

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

Create executable Java applications with a main method

"Hello World!" application

class HelloWorldApp {
  public static void main(String[] args) {
  System.out.println("Hello World!"); // Display the string.

In the Java programming language, every application must contain a main method.

Import other Java packages to make them accessible in your code


  • Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier etc.
  • A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management.

Some of the existing packages in Java are:

  • java.lang - bundles the fundamental classes
  • java.io - classes for input , output functions are bundled in this package

Creating a package:

  • When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.
  • The package statement should be the first line in the source file.

package packageName;

interface interfaceName {

For More and Detailed discussion about “Java Basics” Download Epractize Labs OCAJP 7  Certification Preparation Training Kit.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}