Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Java Inheritance
Java Single Inheritance
Java Super Keyword
Java Multiple Inheritance
Java Hierarchical Inheritance
Java Multilevel Inheritance
Java Method Overriding
Java Abstract Classes,Methods
Java Final Variables, Methods
Java Dynamic Dispatch Methods
Java Visibility Controls

Interfaces

Java Interface
Java Class vs Interface
Java Multiple Inheritance
Java Nested Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Class


What is class in java?

Class is a user defined data type.

All the data/fields and methods/functions are encapsulated within the class. The data and methods written within the class are called as members of class.

When we define any class, we are not defining any data, we just define a structure or a blueprint, as to what the object of that class type will contain and what operations can be performed on that object.

Java Class Structure and Example

Syntax

class className
{
	// instance variable declaration
	type/datatype  variable1;
	type/datatype  variable2;
	.
	.
	.
	type/datatype  variableN;

	//	method declaration
	return_type method_name( parameter list..)
	{
		// body of method
	}
}

Explanation

Instance variable declaration

they are called as instance variables because they are created whenever an object of the class is instantiated. These are accessible to all the functions written inside the class.

Method declaration

Method means functions. Methods are used for working on the data contained in the class.

Example of class

Consider the Class of Cars. There may be many cars with different names and brand but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage etc. So here, Car is the class and wheels, speed limits, mileage are their properties.

class Cars
{
     static byte wheels;
     static int speed_limit, mileage; 

     static void apply_break()
     {
	 System.out.println("Break pressed");
     }
     static void increase_speed()
     {
	 System.out.println("Speed increased");
     }
     public static void main(String args[])
     {
       wheels = 4;
	 speed_limit = 120;
	 mileage = 23;
	 System.out.println("wheels  : "+wheels+" speed limit : " +speed_limit+" mileage : "+mileage);
	 apply_break();	// calling member function
	 increase_speed();
     }
}
	

Output

wheels : 4 speed limit : 120 mileage : 23
Break pressed
Speed increased

In the above example of class Cars, the data member will be speed limit, mileage etc. and member functions can be applying brakes, increase speed etc.


Why static fields and methods?

In java, the main method accepts only static type of data and methods from outside the main method. that’s why we have not to worry about creating an object of same class for calling the methods or initializing the fields.

We will Learn more about static keyword on later tutorial.

<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo