Non-Primitive data types are derived from primary data types. It is used to store a group of values for example Classes, Interface, Array, structure, union, link list, stacks, queues etc.
1. Classes
A class is a collection of objects of a similar type. Once a class is defined, any number of objects can be produced which belong to that class.
Objects are instances of the Class.
Classes and Objects are very much related to each other, without objects, you can't use a class.
Example of Class
package BestJavaTutorial;
class Simple
{
int no1=10, no2=20, ans;
public void sum()
{
ans=no1+no2;
System.out.println("Addition of no1 and no2 is : "+ans);
}
public void sub()
{
ans=no1-no2;
System.out.println("Substraction of no1 and no2 is : "+ans);
}
public static void main(String args[])
{
Simple obj1=new Simple();//first object
Simple obj2=new Simple();//Secound object
obj1.sum();
obj2.sub();
}
}
run:
Addition of no1 and no2 is : 30
Substraction of no1 and no2 is : -10
BUILD SUCCESSFUL (total time: 0 seconds)
Tags:
Core Java