2.1 Class in Java.
Class in Java.
class Program1{
public static void main(String args[]); // ->Java Source File->javac ->Program1.class
{
-
}
}
class Program2
{
public static void main(String args[]); // -> Java Source File->javac ->Program2.class
{
-
}
}
class Program3
{
public static void main(String args[]); // -> Java Source File->javac -> Program3.class
{
-
}
}
Notes :
A Java Source file can have any number of class termination whenever its compile such source file the compiler generate seperate .class file for each java class.
JVM can execute only one class at a time and that class should contain main method defination.
Type of Java Program :
1. Data Type.
class className
{
-
}
2. Interface Type
interface interfaceName
{
-
}
3. enum type.
enum enumName
{
-
}
4. Annotation Type.
@interface annotationName
{
-
}
1. Data Type of Java Program.
class className
{
// declare variable(data member).
// declare method/function (member function). ->member of class can be static or non-static.
}
Data Type of Program :
class Demo1
{
int k =10;
void test();
{
int j =20;
}
}
class sample
{
double j = 1.2;
void print()
{
-
}
}
Notes :
Any thing define in the body of class known as member of the class .
the members can be
Data member.
Member function.
Data member nothing but variable declare in the class body to store the data.
Member function nothing but method declare in the class body which operate on data members.
The member of one class can be access for another class based on the access specifier.
Java supports four type of access specifier.
1. private
2. package level.
3. protected.
4. public.
Members Type in Class. :
A class can have two type of members.
- static ->which is declare by the static modifier/keyword.
-non-static -> which is declare without static modifier.
-> Example Static Members.
class Demo1
{
static int k =10;
static void test()
{
-
}
}
// className.staticMemberName -> Static reference
System.out.println(Demo1.k);
Demo1.test()
// int res = Demo1.k * Demo1.k;
Non-Static Member :
class Demo3
{
int k = 12;
void test()
{
-
}
}
-> need instance to access non-static member.
-> new operator is used to create instance of class.
new className(); // new->operator , className();->constructor.
Member access System.
new className.memberName
System.out.println(new Demo3.k); // new Demo3()->instance , k->data member.
new Demo3.test(); // new Demo3()->instance , test()->member function.
Example Program :
class Demo3
{
int k = 12;
void test()
{
System.out.println("running test().........!");
}
}
class MainClass3
{
public static void main(String args[])
{
System.out.println("program started");
System.out.println(ne Demo3().k);
new Demo3.test();
System.out.println("program ended");
}
}
Example Program4 :
class Demo4
{
static int j = 56;
int k = 12;
void test()
{
System.out.println("running test().........!");
}
static void disp()
{
System.out.println("running disp().........!");
}
}
class MainMethod4
{
public static void main(String args[])
{
System.out.println("program started");
System.out.println(Demo4.j);
Demo4.disp();
System.out.println(new Demo4().k);
new Demo4().test();
System.out.println("Program Ended");
}
}
Comments
Post a Comment