What is Java class?
A class can be defined as a template or blueprint which describes the states and behaviours of an object.
How to declare a class?
A class is declared by the use of the class keyword.
Syntax
class classname { datatype instance-variables; datatype methodname(parameter-list) { // body of the method } }
What does Java Class Consist?
Fields or Variables
Field is nothing but the property of the class or object which we are going to create .
Example: if we are creating a class called computer then they have property like model, mem_size, hd_size, os_type etc.,
Methods
Method is nothing but the operation that an object can perform it define the behavior of object how an object can interact with outside world .startMethod (), shutdownMethod ().
Print the value of variable
public class Demo { public static void main(String [] args) { int a=50; System.out.println(a); } }Try it Yourself
What is Objects in Java?
Object is an instance of the class.It is a bundle of related variables and functions (also known methods).
Objects share three characteristics: They have Identity,State and Behavior.
State: State is a well defined condition of an item. A state captures the relevant aspects of an object.
Behavior: Behavior is the observable effects of an operation or event.
Identity:name of the Object.
Example
Object: House(identity) State: Current Location, Color, Area of House, etc., Behavior: Close/Open main door.
Solve this!!
How to declare a object?
To declare an object you must
- Give the object a name
- Specify what type (i.e. class) the object will be.
Syntax
<class name><object name>; Example: Player player;
How to create a Object?
To create an object,use the syntax
Syntax
<object name> = new <class name> (); Example: creating a Player object player = new Player();
Declaration and creation can be done all in one step.
Example
Player player = new Player();
Example
class Student{ int id; String name; } class Test{ public static void main(String args[]){ Student s1=new Student(); s1.id=100; s1.name="aaa"; System.out.println(s1.id+" "+s1.name); } }Try it Yourself
Solve this!!
class box { int width; int height; int length; } class mainclass { public static void main(String args[]) { box obj = new box(); obj.width = 10; obj.height = 2; obj.length = 10; int y = obj.width * obj.height * obj.length; System.out.print(y); } }
class box { int width; int height; int length; } class mainclass { public static void main(String args[]){ box obj = new box(); System.out.println(obj); } }
Object obj of box class contains reference to the memory which was given to its class instances. Printing obj will print the address of the memory.
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling