- Class Fundamentals
- How Objects are Created
- Reference Variables and Assignments
- Methods
- Constructors
- Default and No-Arg Constructors
- Package
- Import Statements
- The this Keyword
Class Fundamentals
A Java class is often used to represent a concept.

A Java class is often used to store or represent data for the construct that the class represents.
For example, you could create a model (a programmatic representation) of an Employee. An Employee object defined by using this model contains values for employee id (empId), name, social security number (ssn) and salary.
A constructor is used to create an instance of a class. Unlike methods, constructors do not declare a return type, and are declared with the same name as their class. Constructors can take arguments and you can declare more than one constructor.
How Objects are Created
To construct or create an instance (object) of the Employee class, use the new keyword.

- In this fragment of Java code, you construct an instance of the Employee class and assign the reference to the new object to a variable called emp.
- Then you assign values to the Employee object.
Explanations
- You need to allocate memory for the Employee object and call a constructor in the class to initialize all the instance variables of the class.
- To allocate memory, new operator is used and to initialize all the instance variables of the class, the constructor is invoked.
- An instance of an object is created when you use the new keyword with a constructor. All the fields declared in the class are provided with memory space and initialized to their default values.
- If the memory allocation and constructor are successful, a reference to the object is returned as a result. In the example, the reference is assigned to a variable called emp.
- After all the data fields are set with values, you have an instance of an Employee with an empId with a value of 101, name with string John Smith, ssn set to 011-22-3467, and salary with the value of 120, 345.27
Reference Variables and Assignments
Variables in Java are classified into primitive and reference variables. From the programmer’s perspective, a primitive variable’s information is stored as the value of that variable, whereas a reference variable holds a reference to information related to that variable. Reference variables are practically always objects in Java.
Methods
When a class has data fields, a common practice is to provide methods for storing data (setter methods) and retrieving data (getter methods) from the fields.

Constructors

- A constructor is used to create an instance of a class.
- Constructors can take parameters.
- A constructor is declared with the same name as its class.
Default and No-Arg Constructors
- Every class must have at least one constructor.
- If you do not provide any in a class’s declaration, the compiler creates a default constructor that takes no arguments when it is invoked.
- The default constructor initializes the instance variables to the initial values specified in their declarations or to their default values (zero for primitive numeric types, false for boolean values, and null for references).
- If your class declares constructors, the compiler will not create a default constructor.
- In this case, you must declare a no-arg constructor if default initialization is required.
- Like a default constructor, a no-arg constructor is invoked with empty parentheses.
Package
In Java, a package is a group of (class) types. There can be only one package declaration for a file.
Packages create a namespace, a logical collection of things, like a directory hierarchy.
Packages prevent name collision and provide access control to classes. It is a good practice to always use a package declaration.
Import Statements
The import keyword is used to identify classes you want to reference in your class.
- The import statement provides a convenient way to identify classes that you want to reference in your class.

- You can import a single class or an entire package.

- You can include multiple import statements.

- It is good practice to use the full package and class name rather than the wildcard * to avoid class name conflicts.
The this Keyword
The this keyword refers to the current object in a method or constructor.
The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter). If you omit the keyword in the example above, the output would be “0” instead of “5”.
this can also be used to:
- Invoke current class constructor
- Invoke current class method
- Return the current class object
- Pass an argument in the method call
- Pass an argument in the constructor call