Showing posts with label static class. Show all posts
Showing posts with label static class. Show all posts

Thursday, 25 September 2014

Can a class be static in java?

Static class in java :

Static class in java :



YES, we can  have static classes in java like we've got static variables and static method and static things are related to the complete class not with an object of a class.

Similarly ,static classes can even be related to another class. therefore we are able to have solely static nested classes. Static variables and methods
Static variables and methods


Static variables and methods :
Static Variable :Static variables belongs to the complete class instead of one object .
They have only one copy of memory
They can be access with simply name of class ,we don't need an object to initialize or access them
For eg . we've got a class Student as below :
public class Student{
private static String studentId;
public Static void studenttest()
{
}
So we are able to access each static method and variable as
Student.studentId
Student.studenttest()





You may like :
Custom Exception in Java -Tutorial

Read More »

Saturday, 16 August 2014

List in java Collection

List :

It is an interface inheritd from Collections for ordered collection of objects.
It can contain duplicate elements.

How is ArrayList different from Array?


Array
Is not definite in size. Can expand according to requirement.
Is definite in size.
Stores objects
Store primitive data.
Is present in collections
framework Present in lang package

See :How to sort an Array List without using Comparator or sort method


For internal working of Array List Read: Internal worling of ArrayList

How to convert an array to a list?

using Arrays.asList() method.


ArrayList and LinkedList 



Array List and Link List are two classes that implements List Interface .

ArrayList -> is an array based representation of list.
Can be accessed using get and set methods.
Used for simple sequence.

Linked List->
Based on Link List representation.
Give good performance with add() and remove()n methods.
Gives poor performance with get() set() methods.


List Iterator

Implements Iterator interface.

List Iterator methods -> 
1.hasnext()
2. next()
3. hasprevious()
4. previous
5. nextindex();
6. previousindex();
7. remove()
8. add();
9. set()


See :Java Collection-Set
Static varibale,static class,Static method
Read More »