Friday, 15 July 2011

Access modifiers and array of objects



There are three Access Modifiers:-
1.public
2.private
3.protected

and 4 Access controls:-
1.private
2.public
3.protected
4.default/friendly/no modifier

private:-Accessible with in only same class.

default:-this is the by default access of a variable or function.
default are accessible with in the same package.

protected:-accessible in same package or child class.
protected=default+child class
Child class may be in the same or different package.

Note:-We also need to import the package for using the public and protected members in another package.

Note:-class can be public or default. But can’t be private or protected.

Note:-In java only one class can be public and the name of class must be same otherwise you well get compile time error.

Array of Objects:-
First of create a array by writing
Complex[] c=new Complex[n];
n is the size of array of datatype Complex.
Now  use a loop
for(int i=0;i<n;i++)
{
 c[i]=new Complex();
}
This will create a array of objects.
Note:-we if skip c[i]=new Complex(); then we c[i] is referencing to null(by default). So we get null pointer exception.


No comments:

Post a Comment

 

About The Author


I am a B.Tech Computer Science Student. I love Programming and blogging. I am Oracle certified Java Professional. Read More
Indian Blogger

Teachers Jobs

Footer 3

Twitter Facebook RSS YouTube Google
​

All Rights Reserved ©Sun Java Masters 2013