Class in
java
Classes are
fundamental building blocks of JAVA Programming Language.
Try this
program:--
class cls
{
void show()
{
System.out.println(“this
is not a main class”);
}
}
public class
abc
{
public static void main(String
s[])
{
cls obj=new
cls(); //making object of class cls
obj.show(); //calling show() method
}
}
//This file
must be saved with name abc.java never
as cls.java because abc is main class.
Note: -- in
JAVA first later of all predefined classes like (String, System, Scanner) are
capital.
Access specifiers
There are
four types of Access specifiers in JAVA
1>>default
àthis
is visible in classes or sub classes of same package
2>>public àcan be accessed anywhere in program.
3>>protected
àcan
be accessed only in sub / derived classes.
4>>private
àonly
accessible within class never out of class or sub class.
You can use
these Access specifiers by putting them before their name in class, variables,
or methods.
Note:-
there will be only one public class in a package which contains main() method
and file will be saved to that class name.
public int
a; // this is public
private
void show(); //this is private
protected
class cla{} //this is protected
char c; //this is default
Constructor
&& Destructor
Constructor:
--- (features):--
1. It is a special method with same
name as class name
2. It doesn’t have any return type not
even void
3. It can’t be static or final
4. It help in the creation of object
5. It is automatically called when
object gets instantiated.
6. It can be over loaded
Destructor
JAVA doesn’t
have destructor. Role of Garbage
collection is automatically done by JAVA VIRTUL MACHINE (JVM). But we can also
define a method name “finalize” which is automatically called at garbage collection.
class consclass
{
consclass()
//default constructor
{
System.out.println("this
is default constructor");
}
consclass(int
a,int b)
{
System.out.println("this
is parameterized constructor ");
System.out.println("passed
parameters are "+a+" and "+b);
}
public
void finalize()
{
System.out.println("this
is finalize method used to garbage collection");
}
}
public class abc
{
public
static void main(String as[])
{
consclass
obj1=new consclass(); //object of default constructor
consclass
obj2=new consclass(10,20); //object of parameterized constructor
}
}
Static methods
and variable
Static members
(methods and variable) are called without any reference (means without object).
Static method
can only call static variable or static method.
Static variable
or method gets initialized only once.
--> Run these two programs and observe the output:--
Normal variable
and methods
class consclass
{
static
int a=10;
static
int b=20;
static
void input()
{
a=a+1;
b=b-1;
}
static
void show()
{
System.out.println("a=="+a);
System.out.println("b=="+b);
}
}
public class abc
{
public
static void main(String as[])
{
consclass
ob1=new consclass();
consclass
ob2=new consclass();
consclass
ob3=new consclass();
ob1.input();
ob2.input();
ob3.input();
ob1.show();
ob2.show();
ob3.show();
}
Static variable and methods:---
class consclass
{
static
int a=10;
static
int b=20;
static
void input()
{
a=a+1;
b=b-1;
}
static
void show()
{
System.out.println("a=="+a);
System.out.println("b=="+b);
}
}
public class abc
{
public
static void main(String as[])
{
consclass.input();
consclass.input();
consclass.input();
consclass.show();
consclass.show();
consclass.show();
}
No comments:
Post a Comment