top of page

Java Class Declaration

  • javastrokes
  • Sep 24, 2016
  • 2 min read

A program in Java has class definition which contains the code for that program. Java class is like a blue print, template, prototype or prescription for a particular kind of object. You can create any number of object / instance for a specific class type, so the object of a class incorporates all the components specified as belonging to that class.


Java class declaration contains two important things:


Fields: Variables /fields are used to store the data items that typically differentiate one object of the class from another. They are also referred to as data members of a class.


Methods: methods define the operations you can perform for the class. Fields are used by the methods and methods in a class definition are named, self-contained blocks of code.


Classes form the basis of object oriented programming, because classes specify the objects you use in object-oriented programming. Classes are closely related to objects. Real world objects such as books, computers, etc belong to a class so you can create class types to represent these real world objects. For example, you can create a class called Book and make one or more instances/objects for that class type Book. Two different book objects being instances of that same Book class have the same properties, such as bookName, price, author and behaviours /methods, such bookDescription etc. However, the properties and behaviours differ for both book objects.


General syntax for declaring a Class and its Components in Java

Java class fields

Example:

class Employee {

long employeeId;

String employeeName;

String email;

}


As I mentioned above, your class declaration contains two important things, such as fields and methods or behaviours.


Fields in a Class Declaration:

// class declaration…

<<modifiers>> class <<class name>> {


// field declaration syntax

<<modifiers>> <<data type>> <<field name>> = <<initial value>>;

}

Java lets you declare two types of fields for a class:

  • Instance fields

  • Static / Class fields

The fields of your Java class represent the properties of object of that particular class. Here the Employee has three fields, which are representing the properties of object of Employee class.


It means every object of Employee class has three properties: employeeId, employeeName & email So your Employee class declaration should declare these three fields inside the body of the Employee class.

  • Instance fields

// class Employee contains only instance variables (fields)

class Employee {

long employeeId;

String employeeName;

String email;

}


Note: Every instance or object of an Employee class will have a copy of these three fields as these fields are declared as instance variable(not declared using ‘static’ keyword) inside your Employee class.

  • Static / Class fields

// class Employee contains three instance variables & one static variable (fields)

class Employee {

long employeeId;

String employeeName;

String email;

String static department;

}

Apart from instance variables, Sometimes a property called static variable which belongs to the class itself, not to any particular instance of that class. For example, all the instances of Employee class would share the same the copy of these static or class variable. All class variables must be declared using the static keyword as a modifier.


You always specify employee id, employee name and email. All the objects would share and access the same copy of department property.



コメント


Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
Recent Posts
Archive
Search By Tags
  • Grey Facebook Icon
  • Grey Twitter Icon
  • Grey Instagram Icon

© 2016 by JavaStrokes. Proudly created with Wix.com

Success! Message received.

© Copyright 2016 by JavaStrokes
bottom of page