Classes and Objects in Java

Java is a programming language that follows the Object-Oriented Programming (OOP) principles. OOP model focusses on realworld objects to model applications. A real world object will have some properties and some behaviour. For example, human object will have properties like height, weight etc. and behaviours like walk, run, talk, etc.  

 

Classes vs. Objects

Classes (and objects) in Java are used to model real world objects. A class is a blueprint with properties and behaviours. Properties are represented as variables that hold some values and behaviours are represented as methods.

An object is a real representation of a class. For example, Human class may define the properties of a human like height, weight etc. Each individual like you and me are instances (or objects) of that class with specific and different values for these properties.

 

Clearing a class

We can create a Class as:

class Hello {

}

  • Here, class is a keyword, and is followed by a name for your class, and an opening and closing brace to set the boundaries for this class.

  • We can add the optional access modifiers (e.g. public class Hello {}) to the class, which specifies the scope of the class.

  • We need to save this class in a .java file, which is the file extension for java source code file.

 

Rules for declaring classes in a Java file

There are some rules for declaring classes in a java file:

  1. There can be only one public class per source code (.java) file.

  2. If there is a public class in a file, the name of the file must match the name of the public class and it is case sensitive.

  3. A file can have more than one non-public class.

  4. Files with no public classes can have a name that does not match any of the classes in the file.

 

Properties and behavior of a class

There can be two types of members at class level (outside of all methods, but inside of class boundaries {}): instance members and static members.

  • Each object created from a class will have its own copies of instance variables.

  • Static members belong to the class and there will be only one copy of static members shared by all objects.

    • Static members have static mentioned along with their declaration.

  • Class members can be variables and methods; they are explained in future tutorials. 

Example

class MyClass{

 int myInsVar1; //variable

 int myInsVar2; //variable

 static int myStaticVar1;

 static int myStaticVar2;

 public void myMethod1() {

    //some data

 }

public void myMethod2() {

    //some data

 }

}

 

Creating objects

Objects of a class are created using the new keyword followed by a constructor name.

MyClass myClassVar = new MyClass();

  • A constructor is a special method used for initializing an object, with the same name of the class, and without a return type (unlike a method). 

  • If you do not create a constructor, Java will create a default constructor that accepts no parameters and does nothing (empty body). 

  • With the use of new keyword followed by a constructor (new MyClass()), Java allocates memory for the newly created object in the heap memory and return a reference to it which is stored in a reference variable matching the type.

References and notes: 
  • Java keywords are a reserved set of words that java uses in its syntax and are not allowed to be used by us for any other purpose.
  • A public class can be accessed by any class within your application. There are other access modifiers, but a top level class can use only public and default access (no access modifier). A class without any access modifier is called a default access class.
  • Scopes are discussed later after we discuss about blocks and methods.
  • Constructors are discussed in detail later.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)