Saturday, April 01, 2006

Declarations and Access Control


Write code that declares, constructs and initializes arrays of any base type using any of the permitted forms both for declaration and for initialization.

Declaring arrays:

For example,use either

 int[] x;

or

 int x[];

Both are legal positions for the brackets. To declare a multidimensional array, use multiple sets of brackets, e.g. int i[][]; declares a two dimensional array. Note that, for example, int[]i[]; is also legal, and means the same thing. In Java, multidimensional arrays can be "not square" i.e. they are just arrays of arrays, and each of those constituent arrays can be of a different size.

Construct arrays:

Arrays are objects. Use the new keyword to construct them. For example having declared an array i:

int[] i;

you then construct the array and assign it to i as follows:

i = new int[10];

The size of the array is in the brackets (in this example 10). It is common to do both operations in one statement:

int i[] = new int[10];

To initialize an array using loop iteration:

An example:

 int array[] = new int[15];
 for(int j=0; j

Write code to initialize an array using the combined declaration and initialization format:

An example:

char c[]= new char[] {'a','b','c','d','e'};

or you can use

char c[]= {'a','b','c','d','e'};
Declare classes, nested classes, methods, instance variables, static variables and automatic (method local) variables making appropriate use of all permitted modifiers (such as public, final, static, abstract etc.). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.

This is fundamental Java. If you are unsure of this topic, see a textbook.

A good but technical reference for these kinds of issues is the Java Language Specification (not for beginners). It is available at the following URL: http://java.sun.com/docs/books/jls/html/index.html

Some terms and their synonyms:

Scope/Visibility: Where something can be seen / is accessable from.

Nested/Inner Class: A class whose code sits inside the body of another 'outer' class. It exists 'inside' the class in that it can see the private methods and variables.

Instance/Member: Means the method/variable/nested class belongs to each object which is an instance of the class. The term 'a member' is often used to refer to both methods and variables of this type. Cannot be accessed by statically i.e. without having an instance of the class.

Class/Static: The method/variable/nested class belongs to the class as opposed to the instances of the class. Can be used without creating an instance of the class, but static methods/nested class cannot use instancts variables/methods.

Local/Automatic variable: A variable which is declared within a method or as a parameter to the method. Cannot be seen outside the method.

Scoping Types

"default" or "friendly" - this is where no modifier is used. Means something is only visible to classes in the same package.

protected - can only be accessed by classes in the same package or subclasses of this class. This frequently surprises experienced developers, especially those with a prior backround in the mutant hybrid of object orientated programming and assembly language known as 'C++' :-) To fair the name is misleading, as it sounds like it restricts access, where as it in fact it adds subclasses outside the package to the list of things that can access the item in question, as compared to using "default" access.

public - can be accessed by any other class.

private - can only be accessed from inside the class.

Declare classes using the modifiers public, abstract or final:

public - is visible outside of its package. Without this modifier, the class cannot be accessed outside its package.

abstract - cannot be instantiated, is allowed to contain abstract methods.

final - cannot be subsclassed.

Using the modifiers private, protected, public, static, final, native or abstract:

private - can only be accessed from inside the class. Private members are not inherited by subclasses. Inner classes can be declared private.

protected - can only be accessed by classes in the same package or subclasses of this class.

public - can be accessed by any other class.

static - belongs to the class rather than any particular instance of the class. For variables, effectively, there is just one copy of this variable for all instances of the class, and if an instance changes the value, the other instances see that new value. For methods, it means the method can be called without having created an instance, but within the methodd you cannot use the this keyword, or refer to instance variables and methods directly (without creating an instance and referring to the variable/class in that instance). For inner classes, it means they can be instantiated without having an instance of the enclosing class, but as with static methods, the methods of the inner class cannot refer to instance variables or methods of the enclosing class directly.

final - cannot be changed. Variables are constants, methods cannot be overridden, classes cannot be subclassed. Since Java1.1 you can declare the variable without assigning a value. Once you assign a value, you cannot change it. The are known as 'blank' finals, are frequently used for things like constants you wish to initialise from a configuration file.

native - a method which is not written in java and is outside the JVM in a library.

abstract - a method which is not implemented. Must be implemented in a subclass if that subclass is to be 'concrete' and allow people to instantiate it.

Nested Classes

To define a non-static nested class either in a class or method scope:

Place the class definition (for the nested class) inside another class definition (the outer class) or a method.

To define, in method scope, an anonymous nested class that implements a specified interface:

An anonymous nested class is defined where is it instantiated (in a method). An anonymous nested class must either implement an interface or extend a class, but the implements or extends keywords are not used. For example the following line causes the method to return an object which is an instance of an anonymous nested class:

return new SomeClass() { /*body of the anonymous class goes here*/ };

You might like to think of an anonymous nested class as part of a really long new statement, which happens to contain a class definition, and which is why it has a ";" at the end. The following example calls someMethod(), passing an instance of the anonymous nested class:

someMethod(new SomeClass() { /*body of the anonymous class goes here*/ });

In both cases SomeClass() is not the name of the anonymous class (anonymous means it has no name) rather is it the name of the class that you are extending or the interface you are implementing. These classes cannot define a constructor, as they do not have a name that you can use to declare the constructor method. If SomeClass() is a class, the default constructor of that class is called, if you want to use a non-default constructor instead, you supply arguments e.g.:

return new SomeClass(12) { /*body of the anonymous class goes here*/ };

will call the SomeClass constructor which takes an int.

Write code in a non-static method of the outer class to construct an instance of the nested class.

Inner x = new Inner(); constructs an instance of Inner where Inner is a nested class defined in the current class.

Write code to construct an instance on a nested class where either no this object exists, or the current this object is not an instance of the outer class.

You must create an instance of the outer class first. Given a class, Outer, containing a nested class Inner:
Outer.Inner y = new Outer().new Inner();

The above creates an instance of Inner called y, but it had to construct an instance of Outer first. The following example creates the Outer instance on a separate line, the syntax in the second line is the one you use when you already have an instance of the outer class.

Outer x = new Outer();
Outer.Inner y = x.new Inner();
If Inner is static, you can use:
Outer.Inner I= new Outer.Inner();

State which variables and methods in enclosing scopes are accessible from methods of the inner class.

A non-static inner class has access to all member variables and methods of the containing class. If the inner class is defined inside a method, it has access to those method (a.k.a. automatic or local) variables which are declared final, in addition to the above.

A static inner class is restricted in the same way as a static method: it cannot refer to instance variables and methods of the containing class directly (without creating an instance and referring to the variable/class in that instance).

For a given class, determine if a default constructor will be created and if so state the prototype of that constructor.

The default constructor takes no arguments e.g. classname() where classname is the name of you class. A default constructor is automatically created only if you do not create any constructors in your class. If you create a non-default constructor (i.e. one that takes an argument), then you may have to create a default one yourself, if you want there to be one.

All constructors call the default constructor of its parents class (if there is one), and so on up the hierarchy to Object, unless you specify a different constructor using super(...) (to use a constructor in the parent) or this(...). If you use such calls, they must be the first thing in the constructor.


©1999, 2000, 2002 Dylan Walsh. Under free documentation liscence.

61 Comments:

Blogger Mantasha said...

Great site buddy . Really I like ur site I think u r also a Java Developer like me any ways I have some reference books of java which are readable .... Following are the books

Programmer's Guide to Java
Certification A Comp. Primer SE
By Khalid A. Mughal, Rolf W. Rasmussen Publisher : Addison Wesley
Pub Date : August 04, 2003

Java™ Development on PDAs
Building Applications PocketPC
By Daryl Wilding-McBride Publisher : Addison Wesley
Pub Date : June 05, 2003


Learning Java™, 2nd Edition
Publisher : O'Reilly Pub Date : July 2002
Pub Date : June 05, 2003

Jython for Java Programmers
By Robert W. Bill Publisher : New Riders Publishing
Pub Date : December 21, 2001

Enterprise JavaBeans, 3rd Edition
By Richard Monson-Haefel Publisher : O'Reilly
Pub Date : September 2001

Java 1.5 Tiger: A Developer's Notebook
By David Flanagan, Brett McLaughlin Publisher : O'Reilly
Pub Date : June 2004


Java Tutorials Index 1
Java Data Objects
By David Jordan, Craig Russell Publisher : O'Reilly
Pub Date : April 2003

Java™ Extreme Programming Cookbook
By Eric M. Burke, Brian M. Coyner Publisher : O'Reilly
Pub Date : March 2003


Java™ Performance Tuning, 2nd Edition
By Jack Shirazi Publisher : O'Reilly
Pub Date : January 2003

Java™ Performance Tuning, 2nd Edition
By Jack Shirazi Publisher : O'Reilly
Pub Date : January 2003

JavaScript & DHTML Cookbook
By Danny Goodman Publisher : O'Reilly
Pub Date : April 2003

Java Servlet & JSP Cookbook
By Bruce W. Perry Publisher : O'Reilly
Pub Date : January 2004
Java Tutorials Index 2

12:12 PM  
Blogger Nimesh Kumar said...

When preparing for java certification, you must practice mock exams to check your readiness. You can check out some free java certification mock exams to practice. Certification4Career.com provides 500+ free practice questions with web based exam simulator, and most importantly, you can save your test session and later try only incorrectly answered question.

5:05 AM  
Blogger archer612 said...

Good work dude. Your site is very useful for SCJP takers.The exam is definitely challenging and people out there needs to be helped. So how much did you score in the SCJP?

I have my own blog too for SCJP, please inculde this link if you find it useful.

http://www.crack-scjp.blogspot.com

Thanks.

11:34 PM  
Blogger Hapi said...

Hello.. Do you know how to Add Adsense Code Inside Single Post Only in XML Template? Visit your blog to learn how.. Have a nice thursday!

11:13 PM  
Blogger Nicole said...

Download Full Version Software at AriesGames... http://ariesgames.blogspot.com

12:35 AM  
Blogger File said...

Download Forum Poster V3 3.0 at FileAfro.com

http://www.fileafro.com/view_forum-poster-v3-30.html

4:30 AM  
Blogger Hapi said...

hello... hapi blogging... have a nice day! just visiting here....

4:30 AM  
Blogger Keyword said...

hello... you may submit this blog to my webBlog Directory, keyworddir.info.. have a nice day!

Keyword Directory

6:00 PM  
Blogger natasha said...

Watch Natsha Naked!

8:30 AM  
Blogger shailesh said...

Came across a great online learning site to help in Certification. They have lot of Java Programming courses to improve skills at very reasonable prices.

skills2grow.com

6:48 AM  
Blogger indavao said...

Looking for Sex Partner in Davao City? Visit Here!

4:31 PM  
Blogger indavao said...

hi.. just dropping by here... have a nice day! http://kantahanan.blogspot.com

10:24 AM  
Blogger tagskie said...

hi.. just dropping by here... have a nice day! http://kantahanan.blogspot.com/

6:30 PM  
Blogger Alch said...

Hi...Just droppin' by... If you are looking for ways to market your blog, I may help you in some way that I can. Visit Me Here! i will be glad to help you!

4:40 PM  
Blogger JanuskieZ said...

Hi... Looking ways to market your blog? try this: http://bit.ly/instantvisitors

1:30 AM  
Blogger 毛衣 said...

All good things come to an end. ....................................................

10:02 PM  
Blogger 微笑每一天 said...

大奶妹貼圖區0204性影片觀賞露點自拍淫婦女生如何自慰色情站成人笑話av激情網愛視訊美女淫蕩av成人色情電話辣妹視訊聊天性關係情色vcd自慰圖淫美成人論壇台灣色情論壇成人聊天室自拍裸女貼圖視訊成人免費a片影片av成人網成人色情色情台灣辣妹小穴太太陰毛色情訊息裸女自拍色情影片a片論壇性愛技巧美女脫胸罩情色天堂av寫真色情視訊聊天做愛視訊成人影片床上戲情色聊天網火辣情色台灣女優性愛秘笈台灣av女優手淫自慰影片

1:03 AM  
Blogger 惠蘋 said...

每一個人無論怎樣渺小,在自己的眼中,都自有其份量 ..................................................

5:49 PM  
Blogger 雅琳雅琳 said...

It's great!!..........................................

1:53 AM  
Blogger MichellSommerville0202 said...

裸體寫真全裸美女圖片色情訊息黃色圖片自拍裸體圖片sex裸露圖片18限85cc a片台灣色情網站免費色情圖一夜激情聊天情色聊天室限制級爆乳女優作愛巨乳學院性愛情慾陰脣一夜情下體網愛聊天鹹濕做愛自拍成人圖庫成人影城性關係視訊情人性影片觀賞裸照淫美成人論壇av寫真自拍裸女貼圖av圖情色性愛貼圖成人vcdsexy辣妹視訊聊天色情視訊淫婦台灣情色論壇丁字褲貼圖免費a片影片淫蕩女人live show男女做愛火辣妹妹激情網愛聊天美女裸照免費色情網站

4:43 PM  
Blogger ParisH said...

^^ 謝謝你的分享,祝你生活永遠多彩多姿!........................................

11:36 PM  
Blogger 琦竹 said...

Failure is the mother of success...................................................

1:32 AM  
Blogger 欣怡 said...

要持續更新下去喲!!祝你心情愉快.............................................

4:11 PM  
Blogger 怡君 said...

生存乃是不斷地在內心與靈魂交戰;寫作是坐著審判自己。..................................................

7:53 PM  
Blogger 雲亨Ab9雲亨 said...

the food is delicious!............................................................

5:54 PM  
Blogger 趙佳治 said...

想跟你說一聲加油,祝福大家每天開心........................................

3:07 AM  
Blogger 志維 said...

Many a little makes a mickle...................................................................

9:49 PM  
Blogger 雅馨 said...

這樣的情感,真是值得人羨慕.................................................................

10:53 PM  
Blogger 義珊 said...

生活很多細節都要小心點,請多保重 .................................................................

10:54 PM  
Blogger 王瑞 said...

幸福不是一切,人還有責任。....................................................................

5:41 PM  
Blogger 韋以韋以 said...

一個人的價值,應該看他貢獻了什麼,而不是他取得了什麼.................................................................

11:38 PM  
Blogger 丞虹 said...

It takes all kinds to make a world.............................................................

11:27 AM  
Blogger 陳延doraramirezDGD尹 said...

向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。............................................................

6:43 PM  
Blogger 張怡 said...

呵。。。。好可愛~~..................................................................

11:47 PM  
Blogger 原秋原秋 said...

Nothing comes from nothing.............................................................

11:27 PM  
Blogger 淑娟淑娟 said...

安安唷~~幸運的日子送給妳(你)滿滿的幸福,也祝福你天天都開心唷..................................................................

4:03 AM  
Blogger 添d卉王賴uou珮甄賴賴 said...

I have nothing to offer but blood, toil, tears and sweat...................................................................

7:03 PM  
Blogger Kells said...

Thanks for the information. For beginners, Its very important to take note that arrays in Construct are 1-indexed, meaning that the address of the first element is 1 not 0.

9:24 PM  
Blogger 林秋萍林秋萍 said...

人不可以求其備,必捨其所短,取其所長............................................................

3:22 AM  
Blogger 佳皓 said...

好文不寂寞~支持!!!!@@a 搞錯了,這不是論壇推文 XDDD..................................................................

7:35 PM  
Blogger 曾法幸 said...

來逛逛blog~~跟您打聲招呼..................................................................

7:36 PM  
Blogger 琬安琬安 said...

愛情不是慈善事業,不能隨便施捨。............................................................

4:10 PM  
Blogger 童祖如童祖如 said...

上班很累,摸個魚輕輕鬆鬆的逛部落............................................................

5:05 AM  
Blogger 張志亦劉美玲 said...

好的開始並不代表會成功,壞的開始並不代表是失敗..................................................

6:49 AM  
Blogger 姚吳宗瑞家弘 said...

Readiness is all.............................................................

5:24 PM  
Blogger 亦奈美妮 said...

只有尋常才幹,但具有不尋常恆心的人,一切皆可獲取................................................

6:19 PM  
Blogger 邱思吳佳順翰 said...

Rome was not built in a day............................................................

2:02 AM  
Blogger 468ds said...

每次看完你的文章,總是回味許久,要經常發表喔。..................................................

6:56 PM  
Blogger 瑋洪洪洪洪洪婷 said...

與朋友在一起,分擔的痛苦是減半的痛苦,分享的快樂是加倍的快樂。......................................................................

1:56 AM  
Blogger 于庭吳 said...

I do like ur article~!!!............................................................

1:56 AM  
Blogger 江仁趙雲虹昆 said...

學而時習之,不亦樂乎˙ω˙..................................................................

2:52 AM  
Blogger 怡靜怡靜怡靜怡雯 said...

不會從失敗中找尋教訓的人,成功之路是遙遠的。.................................................

8:17 AM  
Blogger 洪勳劉耀德劉耀德華 said...

好文章,希望能一直看到您的PO文..................................................................

7:01 PM  
Blogger 孫邦柔 said...

謝謝大大的分享 我會學會反省與寬容 感恩 ∩△∩............................................................

4:43 AM  
Blogger joven said...

thanks,this is my site
http://kaminyoon.blogspot.com/.. be a follower..more wedding ideas!

5:00 AM  
Blogger joven said...

http://loveumuch143.blogspot.com/..learn how to love more deeper...be a follower..thanks..

5:00 AM  
Blogger HaRmAn said...

http://www.tech-vogue.com/Computers/java%20core/java%20intro.htm

9:41 AM  
Blogger Enrique K Hazelton said...

very good infromation and thanks for sharing this keepitup.
JAVA Online Tutorials

4:28 AM  
Blogger zinavo balaji said...

Nice information for java developers. I learned something and i suggest to people. Bangalore Website Development Company

9:57 AM  
Blogger Rajesh said...

Best Load Runner Training Institute in Chennai,
Load runner Training in Chennai with real time corporate professionals. Our training program is very much mixed both practical and interview point of

questions. Load runner is widely used test automation tool mainly for performance testing.
loadrunnertraining

1:46 AM  
Blogger Rajesh said...

Informatica is one of the most widely used ETL tools for data warehousing and business intelligence. This is the tool that provides DW solution to more than

70% of all DWBI projects today. Informatica is emerging in the market and overcoming all challenges of DWBI compared to any of its competitors.
informatica training in chennai

1:47 AM  

Post a Comment

<< Home