<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;CkYERHY7cSp7ImA9WhRRFUo.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398</id><updated>2011-11-29T20:35:05.809+08:00</updated><category term="abstract" /><category term="Java Keywords" /><category term="object orientation" /><category term="Constants" /><category term="MySQL" /><category term="Final Class" /><category term="slow connection" /><category term="Abstract Class" /><category term="JavaBean Standards" /><category term="arrays" /><category term="Sun Certified Java Programmer" /><category term="static" /><category term="receipt printer" /><category term="synchronized" /><category term="instance variables" /><category term="epson tm 220 usb" /><category term="Access Modifiers" /><category term="local variables" /><category term="epson tm 220" /><category term="Tutorial" /><category term="Java" /><category term="native" /><category term="Class Members" /><category term="shadowing" /><category term="Interfaces" /><category term="inheritance" /><category term="skip name resolve" /><category term="Java Naming Convention" /><category term="strictfp" /><category term="epson" /><category term="Java 1.6" /><category term="volatile" /><category term="final" /><category term="var-args" /><category term="SCJP 1.6" /><category term="Class Access Modifiers" /><category term="Java Identifiers" /><category term="Sun Certified Java Programmer 1.6" /><category term="encapsulation" /><category term="hardware" /><category term="Class Declaration" /><category term="enums" /><category term="transient" /><title>bobhub</title><subtitle type="html">The personal repository of Bob Santos</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://bobhub.blogspot.com/" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>14</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/Bobhub" /><feedburner:info uri="bobhub" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;DEMBRHY-cCp7ImA9WxNbFkg.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-9045481640280810248</id><published>2009-11-19T23:39:00.012+08:00</published><updated>2009-11-20T02:47:35.858+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-20T02:47:35.858+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="SCJP 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="inheritance" /><category scheme="http://www.blogger.com/atom/ns#" term="encapsulation" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="object orientation" /><title>SCJP 1.6 Study Guide: Object Orientation Part 1</title><content type="html">&lt;span style="font-size:180%;"&gt;&lt;span style="font-weight: bold;"&gt;Object Orientation&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;To be a Sun Certified Java Programmer, object-oriented concepts in Java should be second nature to you. In this section we will talk about the OO features of Java like encapsulation, inheritance, and polymorphism. We will continue our discussion in implementing interfaces, return type declarations and static variables and methods. We will also discuss topics like overloading/overriding, casting and coupling and cohesion. This is the second section of this series if you want to read the first section go &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-weight: bold;"&gt;Encapsulation and Inheritance&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Encapsulation.&lt;/span&gt; Encapsulation results to code flexibility and maintainability by hiding implementation details(instance variables) behind public interfaces(methods).&lt;br /&gt;&lt;br /&gt;To implement encapsulation you must do the following:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Hide instance variables by using the &lt;code&gt;private&lt;/code&gt; access modifier.&lt;/li&gt;&lt;li&gt;Create public methods to access private instance variables. This will ensure that users of your classes will go through the method where you can implement checks that will assure you that your classes will be used the right way. These methods are called &lt;span style="font-style: italic;"&gt;getters&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;setters&lt;/span&gt;(some call them &lt;span style="font-style: italic;"&gt;accessors&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;mutators&lt;/span&gt;). For these methods it is recommended to use the JavaBeans naming convention(&lt;code&gt;getXxx&lt;/code&gt; and &lt;code&gt;setXxx&lt;/code&gt;).&lt;/li&gt;&lt;/ul&gt;Example:&lt;br /&gt;(No Encapsulation)&lt;pre class="brush: java"&gt;public class Person{&lt;br /&gt; public double weight;&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class TestPerson{&lt;br /&gt; public static void main(String[] args){&lt;br /&gt;   Person person = new Person();&lt;br /&gt;   person.weight = -1.0; // you probably don't want this to happen&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;(With Encapsulation)&lt;pre class="brush: java"&gt;public class Person{&lt;br /&gt; private double weight;&lt;br /&gt;&lt;br /&gt; // getter&lt;br /&gt; public double getWeight(){&lt;br /&gt;   return weight;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; // setter&lt;br /&gt; public void setWeight(double weight){&lt;br /&gt;   if(weight &gt; 0){&lt;br /&gt;      this.weight = weight; // better, right?&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class TestPerson{&lt;br /&gt; public static void main(String[] args){&lt;br /&gt;   Person person = new Person();&lt;br /&gt;   person.weight = -1.0; // this will compile but&lt;br /&gt;        // will not change the value&lt;br /&gt;        // of weight&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Inheritance.&lt;/span&gt; Inheritance as defined by &lt;a href="http://www.answers.com/main/ntquery?s=inheritance&amp;amp;gwp=13"&gt;Answers.com&lt;/a&gt; refers to the process of genetic transmission of characteristics from parents to offspring. That definition somehow mirrors what inheritance is in Java.&lt;br /&gt;&lt;br /&gt;Inheritance in Java allows one class called the &lt;span style="font-style: italic;"&gt;subclass&lt;/span&gt; to extend another class, the &lt;span style="font-style: italic;"&gt;super class&lt;/span&gt;. Where the subclass by extending the super class inherits some or all of the super class' members(instance variables and instance methods) depending on the &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_09.html"&gt;member's visibility&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;You implement inheritance by using the &lt;code&gt;extends&lt;/code&gt; keyword with the class declaration.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre id="ps" class="brush: java"&gt;public class Person{&lt;br /&gt; private String firstName;&lt;br /&gt;&lt;br /&gt; public void setFirstName(String firstName){&lt;br /&gt;   this.firstName = firstName;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public String getFirstName(){&lt;br /&gt;   return firstName;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class Student extends Person{}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class TestStudent{&lt;br /&gt; public static void main(String[] args){&lt;br /&gt;   Student student = new Student();&lt;br /&gt;&lt;br /&gt;   System.out.println(student.getFirstName());&lt;br /&gt;   // wait Student has no getFirstName()&lt;br /&gt;   // well, Student got it thru inheritance from Person&lt;br /&gt;   student.setFirstName("Bob");&lt;br /&gt;   System.out.println(student.getFirstName());&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;Running &lt;code&gt;TestStudent&lt;/code&gt; results to:&lt;br /&gt;&lt;pre&gt;null&lt;br /&gt;Bob&lt;/pre&gt;A class can only extend one class. You cannot say:&lt;pre class="brush: java"&gt;public class Student extends Person, Animal{}&lt;/pre&gt;The above example will not compile because there's no such thing as multiple inheritance in Java, but you must know that every class that Java programmers create implicitly extends the &lt;code&gt;Object&lt;/code&gt; class.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class TestClass{}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class TestTestClass{&lt;br /&gt; public static void main(String[] args){&lt;br /&gt;   TestClass test = new TestClass();&lt;br /&gt;   System.out.println(test.toString());&lt;br /&gt;   // where did toString() come from?&lt;br /&gt;   // toString() returns a String representation&lt;br /&gt;   // of an object in this case&lt;br /&gt;   // a reference to the object test&lt;br /&gt;&lt;br /&gt;   // let's check with instanceof operator&lt;br /&gt;   if(test instanceof TestClass){&lt;br /&gt;     System.out.println("test is a TestClass");&lt;br /&gt;     // of course we declared it to be&lt;br /&gt;     // a type of TestClass&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   if(test instanceof Object){&lt;br /&gt;     System.out.println("test is an Object");&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;Running &lt;code&gt;TestTestClass&lt;/code&gt; results to:&lt;pre&gt;TestClass@addbf1&lt;br /&gt;test is a TestClass&lt;br /&gt;test is an Object&lt;/pre&gt;Inheritance is usually used for the following purposes:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Code reuse. Which is shown by our &lt;a href="http://www.blogger.com/post-create.g?blogID=8330726088285586398#ps"&gt;&lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt; class&lt;/a&gt;. We created a generic &lt;code&gt;Person&lt;/code&gt; class with a &lt;code&gt;firstName&lt;/code&gt;, &lt;code&gt;getFirstName()&lt;/code&gt; and &lt;code&gt;setFirstName()&lt;/code&gt; and then created a specialized &lt;code&gt;Student&lt;/code&gt; class in which we need a &lt;code&gt;firstName&lt;/code&gt; again but since the &lt;code&gt;Person&lt;/code&gt; class already has them we just need to extend it and we'll be able to set and get the &lt;code&gt;firstName&lt;/code&gt; of any Student object too.&lt;/li&gt;&lt;li&gt;Polymorphism. Polymorphism in Java allows any subclass of a superclass be treated as type of the superclass.&lt;/li&gt;&lt;/ul&gt;Example:&lt;pre class="brush: java"&gt;public abstract class Animal{&lt;br /&gt; public abstract void cry();&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class Dog extends Animal{&lt;br /&gt; public void cry(){&lt;br /&gt;   System.out.println("Aw..Awoo..");&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class Cat extends Animal{&lt;br /&gt; public void cry(){&lt;br /&gt;   System.out.println("Me..Meowwww..");&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class Vet{&lt;br /&gt; public void vaccinate(Animal animal){ // polymorphism in action&lt;br /&gt;   animal.cry();&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class TestVet{&lt;br /&gt; public static void main(String[] args){&lt;br /&gt;   Dog dog = new Dog();&lt;br /&gt;   Cat cat = new Cat();&lt;br /&gt;   Vet vet = new Vet();&lt;br /&gt;&lt;br /&gt;   // polymorphism in action&lt;br /&gt;   vet.vaccinate(dog); // This will compile and run since&lt;br /&gt;   vet.vaccinate(cat); // Dog and Cat are subclasses of Animal&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;Running &lt;code&gt;TestVet&lt;/code&gt; results to:&lt;pre&gt;Aw..Awoo..&lt;br /&gt;Me..Meowwww..&lt;/pre&gt;The example above is patterned from an example in &lt;a href="http://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1258652672&amp;amp;sr=8-1"&gt;Head First Java, 2nd Edition&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;First, we've defined an abstract &lt;code&gt;Animal&lt;/code&gt; class with abstract method &lt;code&gt;cry()&lt;/code&gt;. Next we've defined &lt;code&gt;Dog&lt;/code&gt; and &lt;code&gt;Cat&lt;/code&gt; classes which extends &lt;code&gt;Animal&lt;/code&gt; and both provided an implementation for the &lt;code&gt;cry()&lt;/code&gt; method. We've then created a &lt;code&gt;Vet&lt;/code&gt; class and defined a &lt;code&gt;vaccinate()&lt;/code&gt; method which accepts an object of type &lt;code&gt;Animal&lt;/code&gt;. We declared the method parameter for &lt;code&gt;vaccinate()&lt;/code&gt; to be of type &lt;code&gt;Animal&lt;/code&gt; so that we don't need to create several other &lt;code&gt;vaccinate()&lt;/code&gt; methods each time we come up with a different class that extends the &lt;code&gt;Animal&lt;/code&gt; class. We don't care what class is passed to the &lt;code&gt;vaccinate()&lt;/code&gt; method as long as it can be treated as an &lt;code&gt;Animal&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;Observe that when we ran &lt;code&gt;TestVet&lt;/code&gt;, the JVM still executed the &lt;code&gt;cry()&lt;/code&gt; method of for the &lt;code&gt;Dog&lt;/code&gt; and the &lt;code&gt;Cat&lt;/code&gt; class. We'll discuss why this happened in the next post.&lt;br /&gt;&lt;br /&gt;Polymorphism is implemented in interfaces as well, which means that a class can be considered a type of an interface if that class implements the interface.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public interface Rollable{&lt;br /&gt; void roll();&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class Dog extends Animal implements Rollable{&lt;br /&gt; public void cry(){&lt;br /&gt;   System.out.println("Aw..Awoo..");&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void roll(){&lt;br /&gt;   System.out.println("The dog is rolling...");&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class TestRollableDog{&lt;br /&gt; public static void main(String[] args){&lt;br /&gt;   Dog dog = new Dog();&lt;br /&gt;&lt;br /&gt;   if(dog instanceof Rollable){&lt;br /&gt;     System.out.println("dog is a Rollable");&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;Running &lt;code&gt;TestRollable&lt;/code&gt; results to:&lt;pre&gt;dog is a Rollable&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;IS-A.&lt;/span&gt;For the exam, we need to be able to determine if classes demonstrates IS-A or HAS-A relationship.&lt;br /&gt;&lt;br /&gt;The IS-A relationship is based on class inheritance and interface implementation. You can always test this relationship by using the &lt;code&gt;instanceof&lt;/code&gt; operator.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class Animal{}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public interface Rollable{}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class Dog extends Animal implements Rollable&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class Shitzu extends Dog&lt;/pre&gt;Listing the relationships we have:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;Shitzu extends Dog&lt;/code&gt;, which means &lt;code&gt;Shitzu&lt;/code&gt; IS-A &lt;code&gt;Dog&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Dog extends Animal, so Dog IS-A Animal (we don't really care about grammar here.:D)&lt;/li&gt;&lt;li&gt;&lt;code&gt;Dog implements Rollable&lt;/code&gt;, so &lt;code&gt;Dog&lt;/code&gt; IS-A &lt;code&gt;Rollable&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Since &lt;code&gt;Dog&lt;/code&gt; IS-A &lt;code&gt;Animal&lt;/code&gt; and &lt;code&gt;Shitzu&lt;/code&gt; IS-A &lt;code&gt;Dog&lt;/code&gt; we can say that &lt;code&gt;Shitzu&lt;/code&gt; IS-A &lt;code&gt;Animal&lt;/code&gt;. This only shows that a class has an IS-A relationship with anything further up the inheritance tree.&lt;/li&gt;&lt;li&gt;Q: Can we say &lt;code&gt;Shitzu&lt;/code&gt; IS-A &lt;code&gt;Rollable&lt;/code&gt;?&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-weight: bold;"&gt;HAS-A.&lt;/span&gt; In contrast with IS-A relationship, HAS-A relationship is based on usage/existence/possession instead of inheritance.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class Person{}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class Student extends Person{&lt;br /&gt; private Course course;&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class Course{}&lt;/pre&gt;In the above example we can say that &lt;code&gt;Student&lt;/code&gt; IS-A &lt;code&gt;Person&lt;/code&gt; and at the same time &lt;code&gt;Student&lt;/code&gt; HAS-A &lt;code&gt;Course&lt;/code&gt;. As you might have observed, if class A is an instance variable of class B then class B HAS-A class A.&lt;br /&gt;&lt;br /&gt;That's it for this part we will discuss polymorphism deeper with the next post.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-9045481640280810248?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/YprflL924pdsyiEP0xK_zSjoXTQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/YprflL924pdsyiEP0xK_zSjoXTQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/YprflL924pdsyiEP0xK_zSjoXTQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/YprflL924pdsyiEP0xK_zSjoXTQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/UyNuCeHxtDY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/9045481640280810248/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-object-orientation.html#comment-form" title="42 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/9045481640280810248?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/9045481640280810248?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/UyNuCeHxtDY/scjp-16-study-guide-object-orientation.html" title="SCJP 1.6 Study Guide: Object Orientation Part 1" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>42</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-object-orientation.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEUAQns6fip7ImA9WxNbE0U.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-8800213009866695678</id><published>2009-11-16T16:32:00.006+08:00</published><updated>2009-11-16T23:44:03.516+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-16T23:44:03.516+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="var-args" /><category scheme="http://www.blogger.com/atom/ns#" term="SCJP 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="enums" /><category scheme="http://www.blogger.com/atom/ns#" term="arrays" /><title>SCJP 1.6 Study Guide: Declarations and Access Controls Part 9</title><content type="html">&lt;span style="font-size:130%;"&gt;&lt;span style="font-weight: bold;"&gt;Declaring Arrays, Enums and Methods with Variable Argument Lists (var-args)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is the last part for the Declarations and Access Controls section. If you missed most of the parts of this section please go to the&lt;a href="http://bobhub.blogspot.com/2009/11/scjp-6-study-guide.html"&gt; table of contents&lt;/a&gt; for this study guide series.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Methods with Variable Argument Lists.&lt;/span&gt; Variable argument lists or var-args has been available since Java 5.0. For now, let's focus on declaring var-args. Here are the rules in using var-args:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Just like declaring a regular method parameter, you must specify the type that your var-args will accept. This type maybe primitive or an object type.&lt;/li&gt;&lt;li&gt;To declare a var-args parameter you must follow the type immediately with an ellipsis(...), a space and then the name of the parameter.&lt;/li&gt;&lt;li&gt;It is legal to have other parameters in the method declaration, but there can be only one var-args parameter and it should be the last parameter.&lt;/li&gt;&lt;/ul&gt;Example Legal:&lt;pre class="brush: java"&gt;int addNumbers(int... numbers){}&lt;/pre&gt;&lt;pre class="brush: java"&gt;void storeInformation(String s, int... x){}&lt;/pre&gt;&lt;pre class="brush: java"&gt;void addContacts(String... contacts){}&lt;/pre&gt;Example Illegal:&lt;pre class="brush: java"&gt;int addNumbers(int numbers...){}&lt;/pre&gt;&lt;pre class="brush: java"&gt;void storeInformation(int... x, String x){}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Declaring Arrays.&lt;/span&gt; Arrays are objects that can store multiple variables of the same type or subtypes(polymorphism). Arrays can hold either primitive variables or reference variables. An array is also an object, even if it is declared to hold primitives. For the exam you need to know how to declare arrays, construct arrays and initialize arrays, however, we will focus on declaring arrays first and discuss the remaining two later.&lt;br /&gt;&lt;br /&gt;Just like variables arrays are declared by declaring the type of variables it will hold and a pair of square bracket after the type or after the variable name. It is recommended to put the square brackets right after the type and before the variable name for better readability.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;int[] numbers; // recommended&lt;br /&gt;&lt;br /&gt;int numbers[];  // legal&lt;br /&gt;&lt;br /&gt;String[] names;&lt;br /&gt;&lt;br /&gt;String names[];&lt;br /&gt;&lt;/pre&gt;You can also declare multidimensional arrays, which are simply arrays of arrays.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;int[][] numbers; // two dimensional&lt;br /&gt;&lt;br /&gt;String[][] names[];  // three dimensional&lt;/pre&gt;If you notice the &lt;code&gt;names&lt;/code&gt; array has two square brackets after the type and one after the variable name. This is legal but not recommended.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Declaring Enums. &lt;/span&gt;Enums or items in an enumerated list was made available since Java 5.0. Emuns allows you to restrict a variable's value to just one of a predefined list of enumerated values.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre id="e1" class="brush: java"&gt;enum ShirtSize {SMALL, MEDIUM, LARGE}; // this cannot be private or protected&lt;/pre&gt;In the example above, we've declared an &lt;code&gt;enum&lt;/code&gt; ShirtSize so if we want to set a Shirt to be of small size then we just do this:&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class Shirt{&lt;br /&gt;ShirtSize size;&lt;br /&gt;&lt;br /&gt;public static void main(){&lt;br /&gt; Shirt shirt = new Shirt();&lt;br /&gt; shirt.size = ShirtSize.SMALL;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;The basic component of an &lt;code&gt;enum&lt;/code&gt; is its constants, so the declaration of ShirtSize is the barebones declaration of an enum, and you still can add other declarations.As you may have observed the enum constants are all in uppercase, this is not a requirement but just another area where we use Sun's naming convention for constants.&lt;br /&gt;&lt;br /&gt;Enums can be declared separate from other classes(&lt;a href="http://www.blogger.com/post-edit.g?blogID=8330726088285586398&amp;amp;postID=8800213009866695678#e1"&gt;example&lt;/a&gt;) or within a class as a member but not inside any method.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class Shirt{&lt;br /&gt;enum ShirtSize {SMALL, MEDIUM, LARGE};&lt;br /&gt;&lt;br /&gt;ShirtSize size;&lt;br /&gt;&lt;br /&gt;public static void main(){&lt;br /&gt; Shirt shirt = new Shirt();&lt;br /&gt; shirt.size = Shirt.ShirtSize.SMALL; // enclosing class is required&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;Furthermore, it is also legal to omit the semi-colon after the enum declaration as it is optional.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class Shirt{&lt;br /&gt;enum ShirtSize {SMALL, MEDIUM, LARGE}&lt;br /&gt;&lt;br /&gt;ShirtSize size;&lt;br /&gt;&lt;br /&gt;public static void main(){&lt;br /&gt; Shirt shirt = new Shirt();&lt;br /&gt; shirt.size = Shirt.ShirtSize.SMALL;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt; Enum constants are not types like primitive types and reference types. The &lt;code&gt;enum&lt;/code&gt; constants are different instances of the enum where it is declared. Take a look at the ff for a theoretical perspective of an &lt;code&gt;enum&lt;/code&gt;:&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class ShirtSize{&lt;br /&gt;public static final ShirtSize SMALL = new ShirtSize("S");&lt;br /&gt;public static final ShirtSize MEDIUM = new ShirtSize("M");&lt;br /&gt;public static final ShirtSize LARGE = new ShirtSize("L");&lt;br /&gt;}&lt;/pre&gt; Each enum constant in an enum has its corresponding index which is determined by the order in which they are declared. So in our previous &lt;code&gt;ShirtSize&lt;/code&gt; example &lt;code&gt;SMALL&lt;/code&gt; has an index of 0, &lt;code&gt;MEDIUM&lt;/code&gt;(1) and &lt;code&gt;LARGE&lt;/code&gt;(2).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Enum Constructors, Methods and Variables Declaration.&lt;/span&gt; Since an &lt;code&gt;enum&lt;/code&gt; is a special kind of class you can add constructors, instance variables and methods to its constants. You can also add what we call a &lt;span style="font-style: italic;"&gt;constant specific class body&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;So we've declared the &lt;code&gt;enum ShirtSize&lt;/code&gt; with &lt;code&gt;SMALL&lt;/code&gt;, &lt;code&gt;MEDIUM&lt;/code&gt; and &lt;code&gt;LARGE&lt;/code&gt; constants but how do we know or set the real values of the sizes? Simply by passing a value to a constructor of the &lt;code&gt;enum&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;enum ShirtSize{&lt;br /&gt;SMALL(28), MEDIUM(32), LARGE(36);&lt;br /&gt;&lt;br /&gt;ShirtSize(int inches){ // constructor&lt;br /&gt; this.inches = inches;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private int inches;&lt;br /&gt;&lt;br /&gt;public int getInches(){&lt;br /&gt; return inches;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class Shirt{&lt;br /&gt;private ShirtSize size;&lt;br /&gt;&lt;br /&gt;public void setSize(ShirtSize size){&lt;br /&gt; this.size = size;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public ShirtSize getSize(){&lt;br /&gt; return size;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class TestShirt{&lt;br /&gt;public static void main(String[] args){&lt;br /&gt; Shirt shirt = new Shirt();&lt;br /&gt; shirt.setSize(ShirtSize.SMALL);&lt;br /&gt; System.out.println(shirt.getSize() + " " + shirt.getSize().getInches());&lt;br /&gt; shirt.setSize(ShirtSize.MEDIUM);&lt;br /&gt; System.out.println(shirt.getSize() + " " + shirt.getSize().getInches());&lt;br /&gt; shirt.setSize(ShirtSize.LARGE);&lt;br /&gt; System.out.println(shirt.getSize() + " " + shirt.getSize().getInches());&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;Which prints:&lt;pre&gt;SMALL 28&lt;br /&gt;MEDIUM 32&lt;br /&gt;LARGE 36&lt;/pre&gt;Note that to get all the constants of an &lt;code&gt;enum&lt;/code&gt; you just have to call the &lt;code&gt;values()&lt;/code&gt; method (i.e. &lt;code&gt;ShirtSize.values()&lt;/code&gt;) which returns an array representation of the constants arranged in the order they were declared.&lt;br /&gt;&lt;br /&gt;Things to remember about &lt;code&gt;enum&lt;/code&gt; constructors:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;You can never invoke an &lt;code&gt;enum&lt;/code&gt; constructor directly. It is invoked automatically.&lt;/li&gt;&lt;li&gt;You can define several arguments in an &lt;code&gt;enum&lt;/code&gt; constructor just like a regular class. We will discuss overloading in the next section.&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-weight: bold;"&gt;Constant Specific Class Body.&lt;/span&gt; This is used when you want to override a method in an enum for a particular constant. For example, you want to have the default price for all the sizes except for LARGE to be 300. So you define a method to set the price to 300 and override that method in LARGE to give it a different value.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;enum ShirtSize{&lt;br /&gt;SMALL(28),&lt;br /&gt;MEDIUM(32),&lt;br /&gt;LARGE(36){ // code block that defines the body for this constant&lt;br /&gt; public int getPrice(){ // override the getPrice() method of ShirtSize&lt;br /&gt;  return 500;&lt;br /&gt; }&lt;br /&gt;}; // semi-colon is required to end the constant body&lt;br /&gt;   // when more codes follow&lt;br /&gt;&lt;br /&gt;ShirtSize(int inches){ // constructor&lt;br /&gt; this.inches = inches;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private int inches;&lt;br /&gt;&lt;br /&gt;public int getInches(){&lt;br /&gt; return inches;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public int getPrice(){&lt;br /&gt; return 300;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class TestShirt{&lt;br /&gt;public static void main(String[] args){&lt;br /&gt; Shirt shirt = new Shirt();&lt;br /&gt; shirt.setSize(ShirtSize.SMALL);&lt;br /&gt; System.out.println(shirt.getSize() + " " + shirt.getSize().getPrice());&lt;br /&gt; shirt.setSize(ShirtSize.MEDIUM);&lt;br /&gt; System.out.println(shirt.getSize() + " " + shirt.getSize().getPrice());&lt;br /&gt; shirt.setSize(ShirtSize.LARGE);&lt;br /&gt; System.out.println(shirt.getSize() + " " + shirt.getSize().getPrice());&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;Which prints:&lt;pre&gt;SMALL 300&lt;br /&gt;MEDIUM 300&lt;br /&gt;LARGE 500&lt;/pre&gt;This post ends the Declarations and Access Controls section. Next section will be Object Orientation.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-8800213009866695678?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/NkmvDcCk7HmZ5EAuRwON6maDywM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/NkmvDcCk7HmZ5EAuRwON6maDywM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/NkmvDcCk7HmZ5EAuRwON6maDywM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/NkmvDcCk7HmZ5EAuRwON6maDywM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/tfzbN0qf2Ms" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/8800213009866695678/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_16.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/8800213009866695678?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/8800213009866695678?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/tfzbN0qf2Ms/scjp-16-study-guide-declarations-and_16.html" title="SCJP 1.6 Study Guide: Declarations and Access Controls Part 9" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>1</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_16.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUQESXwycCp7ImA9WxNbE00.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-8956963710709455311</id><published>2009-11-13T15:19:00.007+08:00</published><updated>2009-11-16T00:41:48.298+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-16T00:41:48.298+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="shadowing" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Java 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="SCJP 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="local variables" /><category scheme="http://www.blogger.com/atom/ns#" term="volatile" /><category scheme="http://www.blogger.com/atom/ns#" term="instance variables" /><category scheme="http://www.blogger.com/atom/ns#" term="final" /><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="static" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="transient" /><title>SCJP 1.6 Study Guide: Declarations and Access Controls Part 8</title><content type="html">&lt;span style="font-weight: bold;font-size:130%;" &gt;Variable Declaration&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;When we need to manipulate values provided by the users of our application we can temporarily store them in variables. But to be able to assign values to variables you need to declare themfirst. And when you declare a variable you need to define what type of values that variable can hold and the variable name.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class Person{&lt;br /&gt;int age; // instance variable declaration&lt;br /&gt;         // where int means it can only store int values&lt;br /&gt;String name;&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Primitive and Reference Variable.&lt;/span&gt; We can categorize the &lt;code&gt;age&lt;/code&gt; variable of class &lt;code&gt;Person&lt;/code&gt; as a primitive variable. Primitive variables can be one of 8 types: &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, &lt;code&gt;byte&lt;/code&gt;, &lt;code&gt;short&lt;/code&gt;, &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;long&lt;/code&gt;, &lt;code&gt;float&lt;/code&gt; or &lt;code&gt;double&lt;/code&gt;. How about the &lt;code&gt;name&lt;/code&gt; variable? It was declared to store values of type &lt;code&gt;String&lt;/code&gt;. Whenever a variable is declared to be a type of something other than the primitive types then it's called a reference variable. A reference variable can be used to refer to an object of the declared type or a subtype of the declared type through polymorphism which we will discuss in the next section.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Declaring Primitive Variables.&lt;/span&gt; Primitive variables can be declared as instance variables, class variables(static variables), method parameters or local variables. We will only focus on declaring variables and discuss the several ways of assigning values to them on a later post.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class Student{&lt;br /&gt;private int yearLevel, age;&lt;br /&gt;public static final int TOTAL_NO_OF_YEARS = 4;&lt;br /&gt;&lt;br /&gt;public void setAge(int age){&lt;br /&gt; this.age = age;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public int getRemainingYears(){&lt;br /&gt; int remainingYears = TOTAL_NO_OF_YEARS - yearLevel;&lt;br /&gt; return remainingYears;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;The example above declares primitive variables as instance variable(&lt;code&gt;yearLevel&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt;), class variable(&lt;code&gt;TOTAL_NO_OF_YEARS&lt;/code&gt;), local variable(&lt;code&gt;remainingYears&lt;/code&gt;) and method parameter(&lt;code&gt;age&lt;/code&gt;). Another thing to learn from the example is that you can declare several primitive variables of the same type on one line(like &lt;code&gt;yearLevel&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt;), just separate them with comma.&lt;br /&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="font-weight: bold; text-align: center;"&gt;Type&lt;/td&gt;&lt;td style="font-weight: bold; text-align: center;"&gt;Bits&lt;/td&gt;&lt;td style="font-weight: bold; text-align: center;"&gt;Range&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="font-family: courier new; text-align: center;"&gt;byte&lt;/td&gt;&lt;td style="text-align: center;"&gt;8&lt;/td&gt;&lt;td style="font-family: verdana; text-align: center;"&gt;-2&lt;sup&gt;7&lt;/sup&gt; to 2&lt;sup&gt;7&lt;/sup&gt;-1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;short&lt;/td&gt;&lt;td style="text-align: center;"&gt;16&lt;/td&gt;&lt;td style="font-family: verdana; text-align: center;"&gt;-2&lt;sup&gt;15&lt;/sup&gt; to 2&lt;sup&gt;15&lt;/sup&gt;-1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;int&lt;/td&gt;&lt;td style="text-align: center;"&gt;32&lt;/td&gt;&lt;td style="font-family: verdana; text-align: center;"&gt;-2&lt;sup&gt;31&lt;/sup&gt; to 2&lt;sup&gt;31&lt;/sup&gt;-1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;long&lt;/td&gt;&lt;td style="text-align: center;"&gt;64&lt;/td&gt;&lt;td style="font-family: verdana; text-align: center;"&gt;-2&lt;sup&gt;63&lt;/sup&gt; to 2&lt;sup&gt;63&lt;/sup&gt;-1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;float&lt;/td&gt;&lt;td style="text-align: center;"&gt;32&lt;/td&gt;&lt;td style="text-align: center;"&gt;n/a&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;double&lt;/td&gt;&lt;td style="text-align: center;"&gt;64&lt;/td&gt;&lt;td style="text-align: center;"&gt;n/a&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;The table above shows the range of the six primitive number types. Determining the range of floating point numbers is very complicated, fortunately we don't need to know them for the exam.&lt;br /&gt;&lt;br /&gt;Variables of the &lt;code&gt;boolean&lt;/code&gt; type can only be &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;The &lt;code&gt;char&lt;/code&gt; type may contain a 16-bit Unicode character. The &lt;code&gt;char&lt;/code&gt; variable is really an unsigned 16-bit integer type(0-65535) and can be assigned to any number type large enough to hold 65535.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Declaring Reference Variables.&lt;/span&gt; Like primitive variables, reference variables can be declared as instance variables, class variables, method parameters or local variables.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class Student{&lt;br /&gt;private String firstName, lastName;&lt;br /&gt;public static final String SCHOOL_NAME = "My School";&lt;br /&gt;&lt;br /&gt;public void setFirstName(String firstName){&lt;br /&gt; this.firstName = firstName;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void getFullName(){&lt;br /&gt; String fullName = firstName + " " + lastName;&lt;br /&gt; return fullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;Let's discuss the differences of instance variable, static variable, local variable and method parameter.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Instance Variables.&lt;/span&gt; Instance variables are variables declared inside a class but outside of any methods. When you don't give initial values to instance variables they will be given default values depending on their type.&lt;br /&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="font-weight: bold; text-align: center;"&gt;Type&lt;/td&gt;&lt;td style="font-weight: bold; text-align: center;"&gt;Default Value&lt;br /&gt;&lt;/td&gt;&lt;td style="font-weight: bold; text-align: center;"&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="font-family: courier new; text-align: center;"&gt;byte&lt;/td&gt;&lt;td style="text-align: center;"&gt;0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;short&lt;/td&gt;&lt;td style="text-align: center;"&gt;0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;int&lt;/td&gt;&lt;td style="text-align: center;"&gt;0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;long&lt;/td&gt;&lt;td style="text-align: center;"&gt;0L&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;float&lt;/td&gt;&lt;td style="text-align: center;"&gt;0.0F&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;double&lt;/td&gt;&lt;td style="text-align: center;"&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;char&lt;/td&gt;&lt;td style="text-align: center;"&gt;'\u0000'&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;boolean&lt;/td&gt;&lt;td style="text-align: center;"&gt;false&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;String&lt;/td&gt;&lt;td style="text-align: center;"&gt;null&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;Other Objects&lt;/td&gt;&lt;td style="text-align: center;"&gt;null&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;These are the list of modifiers which can be used in declaring instance variables:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Any of the four access levels (three access modifiers: &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;private&lt;/code&gt; and &lt;code&gt;protected&lt;/code&gt;)&lt;/li&gt;&lt;li&gt;&lt;code&gt;final&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;transient&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;static&lt;/code&gt; (but this variable is already a class variable or a static variable not an instance variable)&lt;/li&gt;&lt;/ul&gt;We'll discuss more on the difference of an instance member(non-static variable and method) and a class member(static variable and method).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Local Variables.&lt;/span&gt; Local variables are variables declared inside a method.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class Student{&lt;br /&gt;String firstName, lastName;&lt;br /&gt;&lt;br /&gt;public void setFirstName(String firstName){ // firstName parameter is&lt;br /&gt; this.firstName = firstName;                // also a local variable&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void getFullName(){&lt;br /&gt; String fullName = firstName + " " + lastName; // fullName is a local variable&lt;br /&gt; return fullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;Method parameters are also considered as local variables since they are declared in the method declaration.&lt;br /&gt;&lt;br /&gt;A local variable lives when declared inside a method and dies when the method has completed. Basically it is already non-accessible outside the method. The following code will not compile.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;public class Student{&lt;br /&gt;String firstName, lastName;&lt;br /&gt;&lt;br /&gt;public void getFullName(){&lt;br /&gt; String fullName = firstName + " " + lastName; // fullName is a local variable&lt;br /&gt; return fullName;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String toString(){&lt;br /&gt; return fullName; // compile error fullName is not declared&lt;br /&gt;                  // inside toString() but inside getFullName()&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt; Unlike instance variables which can have default values, local variables needs to be initialized before it is used by any statement. Usually you give a local variable a value on the same line where you declare it. However, you can initialize a local variable on a different line just as long as it is not used first.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;public class Student{&lt;br /&gt;String firstName, lastName;&lt;br /&gt;&lt;br /&gt;public void getFullName(){&lt;br /&gt; String fullName = firstName + " " + lastName; // fullName is initialized&lt;br /&gt; return fullName;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String toString(){&lt;br /&gt; String fullName; // fullName is declared but not initialized&lt;br /&gt; return fullName; // compile error&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;Local variables can only be marked with the &lt;code&gt;final&lt;/code&gt; modifier.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Shadowing.&lt;/span&gt; Shadowing happens when you try to declare a local variable with the same name as an instance variable. You usually do this in naming method parameters to achieve readability. The following code is an example but will cause what we call &lt;span style="font-style: italic;"&gt;naming collision&lt;/span&gt;. It will still compile but should not be followed.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;public class Student{&lt;br /&gt;String firstName, lastName;&lt;br /&gt;&lt;br /&gt;public void setFirstName(String firstName){&lt;br /&gt; firstName = firstName; // naming collision&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;To resolve naming collision the &lt;code&gt;this&lt;/code&gt; keyword is used to refer to the current active object. Question: Which &lt;code&gt;firstName&lt;/code&gt; is being referred by the two &lt;code&gt;firstName&lt;/code&gt; variables inside &lt;code&gt;setFirstName()&lt;/code&gt;?&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;public class Student{&lt;br /&gt;String firstName, lastName;&lt;br /&gt;&lt;br /&gt;public void setFirstName(String firstName){&lt;br /&gt; this.firstName = firstName; // firstName on the left refers&lt;br /&gt;                    // to the instance variable firstName&lt;br /&gt;                    // since we used the this keyword&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Final Variables.&lt;/span&gt; When a variable is declared with the final keyword you are not allowed to reassign a value to that variable. For primitive variables this means that you cannot alter it's initial value.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;public class Student{&lt;br /&gt;private static final String SCHOOL_NAME = "My School";&lt;br /&gt;&lt;br /&gt;public static String getSchoolName(){&lt;br /&gt; SCHOOL_NAME = "Your School"; // compile error can't reassign value&lt;br /&gt;                              // to final a variable&lt;br /&gt; return SCHOOL_NAME;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;For a reference variable it means you cannot assign it to another object but you still can modify the state (values of the instance variables of the object it refers to). &lt;span style="font-style: italic;"&gt;There are no final objects just final references&lt;/span&gt;. We'll discuss more on this on a later post but here's a quick look:&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;public class Student{&lt;br /&gt;String firstName, lastName;&lt;br /&gt;&lt;br /&gt;public void setFirstName(String firstName){&lt;br /&gt; this.firstName = firstName;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;public class TestStudent{&lt;br /&gt;public static void main(String[] args){&lt;br /&gt; final Student student = new Student();&lt;br /&gt; student.setFirstName("Bob"); // OK to modify firstName&lt;br /&gt;&lt;br /&gt; Student stud = new Student();&lt;br /&gt; student = stud; // compile error&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Transient Variables.&lt;/span&gt; When you mark a variable with the &lt;code&gt;transient&lt;/code&gt; keyword you're telling the JVM to skip that variable when you attempt to serialize the object containing it. Simply put serialization allows you to save an object to a file or send it through a network. We'll discuss serialization on later posts. The &lt;code&gt;transient&lt;/code&gt; keyword can only be used with instance variables.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Volatile Variables.&lt;/span&gt; When a variable is declared with the &lt;code&gt;volatile&lt;/code&gt; modifier it tells the JVM that a thread accessing that variable must always reconcile(check) its own private copy with the master copy. You don't really need to know more about the &lt;code&gt;volatile&lt;/code&gt; keyword except that like the &lt;code&gt;transient&lt;/code&gt; keyword it can only be used with instance variables.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Static Variables and Methods.&lt;/span&gt; Static variables and methods are variables and methods marked with the &lt;code&gt;static&lt;/code&gt; keyword. They are usually called &lt;span style="font-style: italic;"&gt;class members&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Class members exist independent of any instance of the class they are declared in. They are created first before any creation of object of that class. There will be only one copy of each class members regardless of the number of instances of that class.&lt;br /&gt;&lt;br /&gt;The following can be declared as &lt;code&gt;static&lt;/code&gt;:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;methods&lt;/li&gt;&lt;li&gt;variables except local variables&lt;/li&gt;&lt;li&gt;nested class (except classes declared inside a method)&lt;/li&gt;&lt;li&gt;initialization blocks&lt;/li&gt;&lt;/ul&gt;This post is definitely a long one. I suggest to read it a second time and try to code if something is not clear to you. Static variables and methods will be discussed deeper on later posts. The next post will discuss on declaring arrays and enums and it will be the last part for the Declarations and Access Controls section.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-8956963710709455311?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/IjKkWcRqdFaMgjOwsJFpmeUtYcU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IjKkWcRqdFaMgjOwsJFpmeUtYcU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/IjKkWcRqdFaMgjOwsJFpmeUtYcU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IjKkWcRqdFaMgjOwsJFpmeUtYcU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/U1J7drQATn0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/8956963710709455311/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_13.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/8956963710709455311?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/8956963710709455311?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/U1J7drQATn0/scjp-16-study-guide-declarations-and_13.html" title="SCJP 1.6 Study Guide: Declarations and Access Controls Part 8" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>1</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_13.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEQBSH49cSp7ImA9WxNbEEk.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-7187867793191372689</id><published>2009-11-12T22:35:00.005+08:00</published><updated>2009-11-13T01:19:19.069+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-13T01:19:19.069+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="final" /><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="synchronized" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="strictfp" /><category scheme="http://www.blogger.com/atom/ns#" term="SCJP 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="native" /><category scheme="http://www.blogger.com/atom/ns#" term="abstract" /><title>SCJP 1.6 Study Guide: Declarations and Access Controls Part 7</title><content type="html">&lt;span style="font-size:130%;"&gt;&lt;span style="font-weight: bold;"&gt;Class Members and Non-Access Modifiers&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_09.html"&gt;previous&lt;/a&gt; post discussed how to declare class members and how to modify them using access modifiers. Now that you know how visibility affects your code we will take a look at how non-access modifiers affect our code.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;You are already familiar with some of the non-access modifiers we can use with class members, &lt;code&gt;final&lt;/code&gt;, &lt;code&gt;abstract&lt;/code&gt; and &lt;code&gt;strictfp&lt;/code&gt;, but we will also take a look at &lt;code&gt;transient&lt;/code&gt;, &lt;code&gt;synchronized&lt;/code&gt;, &lt;code&gt;native&lt;/code&gt; and &lt;code&gt;static&lt;/code&gt;. We'll discuss how these modifiers affect our code when applied to methods in this post and then to variables in the next post. Static methods and variables will be discussed after the next post.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Final Methods.&lt;/span&gt; When applied to a method, the &lt;code&gt;final&lt;/code&gt; keyword does not allow subclasses to override the method.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class Person{&lt;br /&gt;public final void eat(){&lt;br /&gt; System.out.println("A person eats using his/her mouth.");&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;It is legal to extend &lt;code&gt;Person&lt;/code&gt; but you cannot override the &lt;code&gt;eat()&lt;/code&gt; method.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Final Arguments.&lt;/span&gt; Arguments are the variables we declare inside the parentheses in a method declaration.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class TaxCalculator{&lt;br /&gt;private double taxRate;&lt;br /&gt;&lt;br /&gt;public void setTaxRate(double rate){&lt;br /&gt; rate = rate * 0.1;&lt;br /&gt; taxRate = rate;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;In the example above the argument is &lt;code&gt;rate&lt;/code&gt;. When we declare a method argument to be final we are telling the compiler that &lt;code&gt;rate&lt;/code&gt; cannot be reassigned with a new value.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class TaxCalculator{&lt;br /&gt;private double taxRate;&lt;br /&gt;&lt;br /&gt;public void setTaxRate(double final rate){&lt;br /&gt; rate = rate * 0.1;  // compiler error&lt;br /&gt; taxRate = rate;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Abstract Methods.&lt;/span&gt; An abstract method is generally just a method declaration without any implementation. And if you remember the discussion regarding abstract classes, if a class has an abstract method it should be declared abstract too.&lt;br /&gt;&lt;br /&gt;Once a class extends from an abstract class it is forced to implement that abstract method unless that class is abstract as well. The general rule to follow is: &lt;span style="font-weight: bold; font-style: italic;"&gt;The first concrete(non-abstract) class that extends an abstract class should implement all abstract methods declared in the abstract class&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public abstract class Animal{&lt;br /&gt;private String name;&lt;br /&gt;&lt;br /&gt;public abstract void makeSound();&lt;br /&gt;&lt;br /&gt;public String getName(){&lt;br /&gt; return name;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public abstract class Dog extends Animal{&lt;br /&gt;private Collar collar;&lt;br /&gt;&lt;br /&gt;public Collar getCollar(){&lt;br /&gt; return collar;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// still abstract, no need to implement makeSound()&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class Basenji extends Dog{&lt;br /&gt;// not abstract(concrete) class&lt;br /&gt;// needs to implement bite() from Dog&lt;br /&gt;// no need to really put code here&lt;br /&gt;// just change semi-colon to pair of braces&lt;br /&gt;public void bite(){}&lt;br /&gt;&lt;br /&gt;// also needs to implement makeSound() from Animal&lt;br /&gt;// since Dog did not implement makeSound()&lt;br /&gt;public void makeSound(){&lt;br /&gt; System.out.println("Yodelehihoo");&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;The &lt;code&gt;abstract&lt;/code&gt; keyword when used in a method is not allowed to combine with &lt;code&gt;static&lt;/code&gt;, &lt;code&gt;final&lt;/code&gt; and &lt;code&gt;private&lt;/code&gt; keywords.&lt;br /&gt;&lt;br /&gt;Synchronized Methods. Marking a method with the synchronized keyword makes a method accessible to only one thread at a time. We'll discuss more on this when we get to Threads.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class AccountManager{&lt;br /&gt;public synchronized void withdraw(double value){}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;You can also mix access modifiers with the synchronized keyword. What non-access modifiers can you combine with &lt;code&gt;synchronized&lt;/code&gt;?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Native Methods.&lt;/span&gt; The &lt;code&gt;native&lt;/code&gt; keyword indicates that a method contains platform-dependent code usually in C. You only need to know that this keyword can only be used with methods.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Strictfp Methods.&lt;/span&gt; When you use &lt;code&gt;strictfp&lt;/code&gt; with methods you are sure how your floating points will behave regardless of platform. Check &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_08.html"&gt;Part 4&lt;/a&gt; for details.&lt;br /&gt;&lt;br /&gt;You cannot use &lt;code&gt;strictfp&lt;/code&gt; with variables.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Constructor Declarations.&lt;/span&gt; When you create a Java application you will make, instantiate or create objects to communicate with each other and when you create an object at least one constructor is invoked.&lt;br /&gt;&lt;br /&gt;Every class in Java has a constructor. The compiler will provide one(default constructor) for you if you don't. There are several things to consider in declaring a constructor which we will discuss on a later post. But for now, let's take a look at a basic constructor declaration:&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;public class Person{&lt;br /&gt;public Person(){} // this is a valid Person constructor&lt;br /&gt;&lt;br /&gt;public void Person(){} // this is a valid but badly named method&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;If you notice, the &lt;code&gt;Person()&lt;/code&gt; constructor looks like the &lt;code&gt;Person()&lt;/code&gt; method. The main difference is that constructors cannot have a return type. Constructors also must have the same name as the class in which they are declared.&lt;br /&gt;&lt;br /&gt;Constructors can be marked with access modifiers. Non-access modifiers like &lt;code&gt;static&lt;/code&gt;, &lt;code&gt;final&lt;/code&gt; and &lt;code&gt;abstract&lt;/code&gt; cannot be used with constructors.&lt;br /&gt;&lt;br /&gt;Be sure that you have understood what access and non-access modifiers we can use with methods and how they affect those methods. Try using and combining them as practice. The next post will handle variable declarations.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-7187867793191372689?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/6G2nVcpOUYbG3gOaaB0AyXkh5EI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6G2nVcpOUYbG3gOaaB0AyXkh5EI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/6G2nVcpOUYbG3gOaaB0AyXkh5EI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6G2nVcpOUYbG3gOaaB0AyXkh5EI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/z4lexML22KA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/7187867793191372689/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_12.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/7187867793191372689?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/7187867793191372689?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/z4lexML22KA/scjp-16-study-guide-declarations-and_12.html" title="SCJP 1.6 Study Guide: Declarations and Access Controls Part 7" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_12.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEUFQ3w8eyp7ImA9WxNbEEk.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-6351310539525424710</id><published>2009-11-09T17:44:00.013+08:00</published><updated>2009-11-13T01:16:52.273+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-13T01:16:52.273+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Class Members" /><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="SCJP 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Access Modifiers" /><title>SCJP 1.6 Study Guide: Declarations and Access Controls Part 6</title><content type="html">&lt;span style="font-size:130%;"&gt;&lt;span style="font-weight: bold;"&gt;Declaring Class Members and Access Modifiers&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;By this time I hope you have grasped all the required concepts in &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_06.html"&gt;declaring and modifying classes&lt;/a&gt;. If not go &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_06.html"&gt;back&lt;/a&gt; and I suggest you try coding at the same time. Those concepts are necessary before reading the next posts regarding declaring class members.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Class Members.&lt;/span&gt; When we mention class members we are talking about instance variables, static variables, instance methods and static variables.&lt;br /&gt;&lt;br /&gt;Both instance and static variables are variables declared inside a class but outside any methods. The most obvious difference that you will see between instance and static variables is that static variables are variables declared with the &lt;code&gt;static&lt;/code&gt; keyword, which is the same with instance and static methods.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;public class Student{&lt;br /&gt; public int yearLevel;  // instance variable&lt;br /&gt; public static int noOfStudents;  // static variable&lt;br /&gt;&lt;br /&gt; public void doHomework(){}  // instance method&lt;br /&gt; public static void getNoOfStudents(){}  // static variables&lt;br /&gt;}&lt;/pre&gt; We'll go deeper with static variables and methods in later posts.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Access Modifiers.&lt;/span&gt;  As we've discussed in an &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_06.html"&gt;earlier post&lt;/a&gt;, Java has four access control levels (public, private, protected and default access), but only three modifiers (&lt;code&gt;public&lt;/code&gt;, &lt;code&gt;private&lt;/code&gt; and &lt;code&gt;protected&lt;/code&gt;). You get the default access when you don't specify any access modifier.&lt;br /&gt;&lt;br /&gt;Unlike classes where you can only have two access control levels (public and default), you can have all four with class members.&lt;br /&gt;&lt;br /&gt;The following are the things you should remember when a class (class A) has access to members of another class (class B):&lt;br /&gt;&lt;ul&gt;&lt;li&gt;A method in class A has access to class members of class B.&lt;pre id="pub1" class="brush: java"&gt;package bobhub;&lt;br /&gt;public class B{&lt;br /&gt; public int x;&lt;br /&gt;&lt;br /&gt; public void doBThings(){}&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;package scjp;&lt;br /&gt;import bobhub.B;&lt;br /&gt;public class A{&lt;br /&gt; public void doAThings{&lt;br /&gt;   B b = new B();&lt;br /&gt;   b.x = 2;  // doBThings has access to x from a reference of class B&lt;br /&gt;   b.doBThings();  // doBThings can invoke doAThings() from a reference of class B&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;What class members are inherited by class A when it extends class B.&lt;pre id="pub2" class="brush: java"&gt;package bobhub;&lt;br /&gt;public class B{&lt;br /&gt; public int x;&lt;br /&gt;&lt;br /&gt; public void doAThings(){}&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;package scjp;&lt;br /&gt;import bobhub.B;&lt;br /&gt;public class A extends B{&lt;br /&gt; public void doAThings{&lt;br /&gt;   doBThings(); // since A extends B and it inherits doBThings()&lt;br /&gt;                // it can invoke doBThings() as if it is declared in&lt;br /&gt;                // class A. Sometimes you can use the this reference&lt;br /&gt;                // for clarity. e.g. this.doBThings();&lt;br /&gt;   x = 24;  // A also inherits x from B&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;Now that we have access modifiers for classes and for its members we must consider combination of access modifiers. Of course, you need to take a look at the class first, if class B is not visible to class A then all the members of class B is not visible to class A. When class B is visible to class A that's the time you need to take a look at the visibility of each member of class B. For the next post let's assume that all classes are visible unless stated in the examples.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Public Members.&lt;/span&gt; A class member is considered a public member when it is declared with the &lt;code&gt;public&lt;/code&gt; keyword (e.g. &lt;code&gt;public String name; public void doSomething();&lt;/code&gt;).&lt;br /&gt;&lt;br /&gt;A public member is accessible to all other classes regardless of package(&lt;a href="http://www.blogger.com/post-edit.g?blogID=8330726088285586398&amp;amp;postID=6351310539525424710#pub1"&gt;See example&lt;/a&gt;). Public members are inherited by subclasses of its class(&lt;a href="http://www.blogger.com/post-edit.g?blogID=8330726088285586398&amp;amp;postID=6351310539525424710#pub2"&gt;See example&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Private Members.&lt;/span&gt; Declaring class members with the &lt;code&gt;private&lt;/code&gt; keyword makes them inaccessible to any class except to the class where it was declared.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;package bobhub;&lt;br /&gt;public class B{&lt;br /&gt; private int x;&lt;br /&gt;&lt;br /&gt; private void doBThings(){}&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;package scjp;&lt;br /&gt;import bobhub.B;&lt;br /&gt;public class A extends B{&lt;br /&gt; public void doAThings{&lt;br /&gt;   B b = new B();&lt;br /&gt;   b.x = 24;  // compiler error&lt;br /&gt;&lt;br /&gt;   doBThings();  // compiler error&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;code&gt;Class B&lt;/code&gt; above compiles just fine but we will run through an error in &lt;code&gt;A&lt;/code&gt; when it tries to access &lt;code&gt;x&lt;/code&gt;(&lt;code&gt;b.x = 24;&lt;/code&gt;), since &lt;code&gt;x&lt;/code&gt; is declared with the &lt;code&gt;private&lt;/code&gt; keyword &lt;code&gt;B&lt;/code&gt; is the only class allowed to access &lt;code&gt;x&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;Another line which will result into a compiler error is when &lt;code&gt;doAThings()&lt;/code&gt; method tries to invoke &lt;code&gt;doBThings()&lt;/code&gt; method because subclasses do not inherit private members of its superclass. However, you can declare a method in &lt;code&gt;A&lt;/code&gt; with the same signature as &lt;code&gt;doBThings()&lt;/code&gt; method but it's not considered as overriding(We will discuss more on overriding on later posts). Try this in coding.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Default Members.&lt;/span&gt; When you don't explicitly provide an access modifier you get the default access. Default members may be accessed only by classes in the same package where you declared the members.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;package bobhub;&lt;br /&gt;public class B{&lt;br /&gt; int x;&lt;br /&gt;&lt;br /&gt; void doBThings(){}&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;package scjp;&lt;br /&gt;import bobhub.B;&lt;br /&gt;public class A extends B{&lt;br /&gt; public void doAThings{&lt;br /&gt;   B b = new B();&lt;br /&gt;   b.x = 24;  // compiler error&lt;br /&gt;&lt;br /&gt;   doBThings();  // compiler error&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;Accessing &lt;code&gt;x&lt;/code&gt; in &lt;code&gt;doAThings()&lt;/code&gt; method results in compiler error since &lt;code&gt;x&lt;/code&gt; has default access and &lt;code&gt;A&lt;/code&gt; is in a different package than &lt;code&gt;B&lt;/code&gt;. Also invoking &lt;code&gt;doBThings()&lt;/code&gt; thruough inheritance will result to compiler error because just like private members, a subclass does not inherit protected members of its superclass.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Protected Members. &lt;/span&gt;Members declared with the protected keyword behaves like default members most of the times except with inheritance. Protected members like default members are accessible only to classes within the same package AND also are visible through inheritance by subclasses even if they are on a different package.&lt;br /&gt;&lt;br /&gt;Example:&lt;pre class="brush: java"&gt;package bobhub;&lt;br /&gt;public class B{&lt;br /&gt; protected int x;&lt;br /&gt;&lt;br /&gt; protected void doBThings(){}&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;package scjp;&lt;br /&gt;import bobhub.B;&lt;br /&gt;public class A extends B{&lt;br /&gt; public void doAThings{&lt;br /&gt;   B b = new B();&lt;br /&gt;   b.x = 24;  // compiler error&lt;br /&gt;&lt;br /&gt;   doBThings();  // compiles&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;Accessing &lt;code&gt;x&lt;/code&gt; in &lt;code&gt;doAThings()&lt;/code&gt; method using a reference to an object B results in compiler error since &lt;code&gt;x&lt;/code&gt; has protected access and &lt;code&gt;A&lt;/code&gt; is in a different package than &lt;code&gt;B&lt;/code&gt;. However, invoking &lt;code&gt;doBThings()&lt;/code&gt; through inheritance will NOT result to compiler error since subclasses, even if they are in a different package, will inherit its superclass' protected members.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Local Variables and Access Modifiers.&lt;/span&gt; Local variables are variables usually inside a method. Local variables CANNOT be marked with access modifiers. And there is only one modifier you can use with local variables and that is the &lt;code&gt;final&lt;/code&gt; keyword.&lt;br /&gt;&lt;br /&gt;Below is a table from &lt;a href="http://www.amazon.com/SCJP-Certified-Programmer-Java-310-065/dp/0071591060"&gt;SCJP 1.6 Study Guide Book&lt;/a&gt; to summarize visibility of class members using access modifiers.&lt;br /&gt;&lt;span style="text-align: center;"&gt;&lt;table&gt;&lt;br /&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;Visibility&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;Public&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;Protected&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;&lt;em&gt;Default&lt;/em&gt;&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;Private&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;Same class&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;Any class in the same package&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;A subclass in the same package&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;A subclass outside the same package&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;A non-subclass outside the same package&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We will discuss class members and non-access modifiers in the &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_12.html"&gt;next&lt;/a&gt; post.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-6351310539525424710?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/tTuD8zzE85LLXsPad0UUnHTgvjw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/tTuD8zzE85LLXsPad0UUnHTgvjw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/tTuD8zzE85LLXsPad0UUnHTgvjw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/tTuD8zzE85LLXsPad0UUnHTgvjw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/R-pz7LPOPfs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/6351310539525424710/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_09.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/6351310539525424710?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/6351310539525424710?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/R-pz7LPOPfs/scjp-16-study-guide-declarations-and_09.html" title="SCJP 1.6 Study Guide: Declarations and Access Controls Part 6" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_09.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkIFRnY9fSp7ImA9WxNUGUg.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-5535512997515149324</id><published>2009-11-08T21:59:00.009+08:00</published><updated>2009-11-12T00:55:17.865+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-12T00:55:17.865+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="SCJP 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Constants" /><category scheme="http://www.blogger.com/atom/ns#" term="Interfaces" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer 1.6" /><title>SCJP 1.6 Study Guide: Declarations and Access Controls Part 5</title><content type="html">&lt;span style="font-size:130%;"&gt;&lt;span style="font-weight: bold;"&gt;Declaring Interfaces&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_06.html"&gt;Part 3&lt;/a&gt; and &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_08.html"&gt;Part 4&lt;/a&gt; of Declarations and Access Controls discussed how we can declare and modify classes. This part will turn our attention to declaring interfaces.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;Interfaces are 100% &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_08.html"&gt;abstract classes&lt;/a&gt;, they define abstract methods which creates a contract on what a class can do without imposing how the class will do it. Classes do not extend interfaces they implement them and when they implement interfaces they should implement the methods defined in the interfaces. Interfaces are declared using the &lt;code&gt;interface&lt;/code&gt; keyword.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;package bobhub.food;&lt;br /&gt;public interface Edible{&lt;br /&gt; void cook();&lt;br /&gt; void prepare();&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;package bobhub.food;&lt;br /&gt;public class Apple implements Edible{&lt;br /&gt; public void cook(){&lt;br /&gt;   // you don't need to actually write code here&lt;br /&gt;   // you just need to change the semi-colon to&lt;br /&gt;   // a pair of braces&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void prepare(){&lt;br /&gt;   System.out.println("Slice... Slice... Slice...");&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;When I said earlier that interfaces are 100% abstract classes I mean you can only define &lt;span style="font-weight: bold;"&gt;abstract methods and constants&lt;/span&gt; in an interface, no more no less. The following are rules on declaring interfaces:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;An interface must be declared with the &lt;code&gt;interface&lt;/code&gt; keyword, not the class keyword.&lt;/li&gt;&lt;li&gt;An interface can extend one or more interfaces (comma-separated).&lt;/li&gt;&lt;li&gt;An interface can only extend another interface.&lt;/li&gt;&lt;li&gt;An interface cannot implement anything (A class or another interface).&lt;/li&gt;&lt;li&gt;Interfaces can have &lt;code&gt;public&lt;/code&gt; or default access.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;All interface methods are implicitly &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;abstract&lt;/code&gt;. Take a look at the &lt;code&gt;prepare()&lt;/code&gt; method of the &lt;code&gt;Edible&lt;/code&gt; interface:&lt;pre class="brush: java"&gt;void prepare();&lt;/pre&gt;We can only define abstract methods in an interface but why is the &lt;code&gt;prepare()&lt;/code&gt; method defined without the &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;abstract&lt;/code&gt; keywords? Since interface methods are implicitly &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;abstract&lt;/code&gt; we don't need to mark interface methods with those keywords. Marking them with &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;abstract&lt;/code&gt; is legal but redundant. So we can define the prepare() method like these:&lt;pre class="brush: java"&gt;public abstract void prepare();&lt;/pre&gt;OR&lt;pre class="brush: java"&gt;public void prepare();&lt;/pre&gt;OR&lt;pre class="brush: java"&gt;abstract void prepare()&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Interface methods cannot be marked with these keywords: &lt;code&gt;static&lt;/code&gt;, &lt;code&gt;final&lt;/code&gt;, &lt;code&gt;strictfp&lt;/code&gt; or &lt;code&gt;native&lt;/code&gt;.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;All variables defined in an interface should be and implicitly &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;static&lt;/code&gt; and &lt;code&gt;final&lt;/code&gt;, which means they should all be constants. The following are valid interface constant declarations:&lt;pre class="brush: java"&gt;int MINIMUM_PREPARATION_TIME;&lt;/pre&gt;OR&lt;pre class="brush: java"&gt;final int MINIMUM_PREPARATION_TIME;&lt;/pre&gt;OR&lt;pre class="brush: java"&gt;static int MINIMUM_PREPARATION_TIME;&lt;/pre&gt;OR&lt;pre class="brush: java"&gt;public int MINIMUM_PREPARATION_TIME;&lt;/pre&gt;OR&lt;pre class="brush: java"&gt;static final int MINIMUM_PREPARATION_TIME;&lt;/pre&gt;OR&lt;pre class="brush: java"&gt;public final int MINIMUM_PREPARATION_TIME;&lt;/pre&gt;OR&lt;pre class="brush: java"&gt;public static int MINIMUM_PREPARATION_TIME;&lt;/pre&gt;OR&lt;pre class="brush: java"&gt;public static final int MINIMUM_PREPARATION_TIME;&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;When a class implements an interface it inherits all the constants from the interface. But remember that you cannot change the value of a constant. Also, we can interchange the order of the modifiers we use. The Apple class in the following code will not compile:&lt;br /&gt;&lt;pre class="brush: java"&gt;package bobhub.food;&lt;br /&gt;public interface Edible{&lt;br /&gt; int MINIMUM_PREPARATION_TIME = 60;&lt;br /&gt; void prepare();&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;package bobhub.food;&lt;br /&gt;public class Apple implements Edible{&lt;br /&gt; public void prepare(){&lt;br /&gt;   MINIMUM_PREPARATION_TIME = 120;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;That's it for now for interfaces but we will discuss them again when we talk about Polymorphism as we will learn that interface types can be used polymorphically. In the &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_09.html"&gt;next&lt;/a&gt; post we will learn how we can add class members(instance and static variables and methods) and modify them with access modifiers.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-5535512997515149324?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/hTfkf0CBRZmI6x9BNnCV2H6W8sc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hTfkf0CBRZmI6x9BNnCV2H6W8sc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/hTfkf0CBRZmI6x9BNnCV2H6W8sc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hTfkf0CBRZmI6x9BNnCV2H6W8sc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/-9GsTHSpydo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/5535512997515149324/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_8808.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/5535512997515149324?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/5535512997515149324?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/-9GsTHSpydo/scjp-16-study-guide-declarations-and_8808.html" title="SCJP 1.6 Study Guide: Declarations and Access Controls Part 5" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_8808.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0IER308fip7ImA9WxNUGEs.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-8693923190055293171</id><published>2009-11-08T12:33:00.007+08:00</published><updated>2009-11-11T00:11:46.376+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-11T00:11:46.376+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="strictfp" /><category scheme="http://www.blogger.com/atom/ns#" term="SCJP 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Abstract Class" /><category scheme="http://www.blogger.com/atom/ns#" term="Final Class" /><title>SCJP 1.6 Study Guide: Declarations and Access Controls Part 4</title><content type="html">&lt;span style="font-size:130%;"&gt;&lt;span style="font-weight: bold;"&gt;Final Class and Abstract Class&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We'll pick it right off &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_06.html"&gt;Part 3&lt;/a&gt; of Declarations and Access Controls where we discussed how a regular class is declared in Java and the 2 access modifiers we can define for our classes. With this post, I hope you will learn more about final classes and abstract classes.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;In addition to access modifiers you can still modify a class by using non-access modifiers like &lt;code&gt;strictfp&lt;/code&gt;, &lt;code&gt;final&lt;/code&gt; and &lt;code&gt;abstract&lt;/code&gt; keywords. But remember that you can't always mix non-access modifiers. You can combine &lt;code&gt;strictfp&lt;/code&gt; with either &lt;code&gt;final&lt;/code&gt; or &lt;code&gt;abstract&lt;/code&gt; but you cannot combine &lt;code&gt;final&lt;/code&gt; and &lt;code&gt;abstract&lt;/code&gt; as you will learn later.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Final Class.&lt;/span&gt; When you use the final keyword in a class declaration you define that class to be a final class which means it cannot be subclassed. Any attempt to extend a final class will result into a compiler error. Also, since you cannot subclass a final class, you won't be able to override its methods (Overriding is discussed in Section 2: Object Orientation).&lt;br /&gt;&lt;br /&gt;So when do you a class final? If you look at the javadocs of some classes of the Java core libraries you will see that most are declared to be final which guarantees that those classes will not be modified by others so that they will behave the same on all applications that use them. Therefore, if you depend heavily on the implementations of a class and you want it to behave exactly the same every time it is used then most probably you want that class to be a final class.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;package bobhub.animal;&lt;br /&gt;public final class Animal{&lt;br /&gt;public void eat(){}&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;package bobhub.animal.mammals;&lt;br /&gt;public class Dog extends Animal{}&lt;/pre&gt;&lt;br /&gt;The Animal class will compile, but if you try compiling the Animal class you will get a compiler error. Try it to see the error. In the course of your development, you will rarely make a final class.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Abstract Class.&lt;/span&gt; When we use the word abstract as an adjective we usually mean to say it's something generic.&lt;br /&gt;&lt;blockquote&gt;An abstract object is an object which does not exist at any particular time or place, but rather exists as a type of thing.-&lt;a href="http://www.answers.com/topic/abstract-object"&gt;Answers.com&lt;/a&gt;&lt;/blockquote&gt;Abstract classes are classes that you cannot instantiate. Instead you extend them. And when you are brave enough to instantiate an abstract class the compiler will curse at you with a compiler error.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;package bobhub.animal;&lt;br /&gt;public abstract class Animal{&lt;br /&gt;public String name;&lt;br /&gt;public double height;&lt;br /&gt;&lt;br /&gt;public abstract void eat();&lt;br /&gt;public abstract void sleep();&lt;br /&gt;}&lt;/pre&gt;&lt;pre id="dog" class="brush: java"&gt;package bobhub.animal;&lt;br /&gt;public class Dog extends Animal{&lt;br /&gt;public void eat(){&lt;br /&gt;System.out.println("Eating...");&lt;br /&gt;}&lt;br /&gt;public void sleep(){}&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="brush: java"&gt;package bobhub.test;&lt;br /&gt;import bobhub.animal.Animal;&lt;br /&gt;import bobhub.animal.Dog;&lt;br /&gt;public class TestAnimal{&lt;br /&gt;public static void main(String[] args){&lt;br /&gt; Animal animal = new Animal();&lt;br /&gt; Dog dog = new Dog();&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;The above classes will compile except for &lt;code&gt;TestAnimal&lt;/code&gt; where we tried to instantiate the &lt;code&gt;Animal&lt;/code&gt; class. If you delete that line the &lt;code&gt;TestAnimal&lt;/code&gt; class will compile.&lt;br /&gt;&lt;br /&gt;Let's take a closer look at the Animal class. If you notice the &lt;span style="font-weight: bold;"&gt;&lt;code&gt;eat()&lt;/code&gt; method was declared with the abstract keyword and ends in a semi-colon instead of a pair of braces.&lt;/span&gt;&lt;pre class="brush: java"&gt;public abstract void eat();&lt;/pre&gt;The &lt;code&gt;eat()&lt;/code&gt; method is considered an abstract method since it was declared with the abstract keyword. If you see an abstract method in a class then the class should be declared abstract as well or else you will get a compiler error. When a class extends an abstract class with abstract methods the extending class should implement those abstract methods where you will define how the method should work (See &lt;a href="http://www.blogger.com/post-create.g?blogID=8330726088285586398#dog"&gt;example&lt;/a&gt; above). Moreover, an abstract class can define non-abstract methods as well. We will discuss abstract methods more on later posts.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;strictfp.&lt;/span&gt; The &lt;code&gt;strictfp&lt;/code&gt; keyword is used to modify a class or a method and never a variable. Floating points in a Java application may behave differently in different platforms but declaring a class as &lt;code&gt;strictfp&lt;/code&gt; will make all methods in the class conform to the IEEE 754 standard rules for floating points. If you don't define a class as &lt;code&gt;strictfp&lt;/code&gt; you can make individual methods as strictfp.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;strictfp class Calculator{}&lt;/pre&gt;&lt;pre class="brush: java"&gt;class TaxAnalyzer{&lt;br /&gt;public strictfp analyzeTax(){}&lt;br /&gt;}&lt;/pre&gt;This post ends the topic on how we declare and modify Java classes. The next posts will talk about &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_8808.html"&gt;declaring interfaces&lt;/a&gt;. See you until then!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-8693923190055293171?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/NZkl4_KBa8Ooe95rhgnuBbg2-D4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/NZkl4_KBa8Ooe95rhgnuBbg2-D4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/NZkl4_KBa8Ooe95rhgnuBbg2-D4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/NZkl4_KBa8Ooe95rhgnuBbg2-D4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/hJ4SR_E7AZw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/8693923190055293171/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_08.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/8693923190055293171?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/8693923190055293171?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/hJ4SR_E7AZw/scjp-16-study-guide-declarations-and_08.html" title="SCJP 1.6 Study Guide: Declarations and Access Controls Part 4" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_08.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0YHQHw9eCp7ImA9WxNUFkg.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-1767540971279258036</id><published>2009-11-06T00:55:00.013+08:00</published><updated>2009-11-08T13:45:31.260+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-08T13:45:31.260+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="Class Declaration" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="SCJP 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Class Access Modifiers" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Access Modifiers" /><title>SCJP 1.6 Study Guide: Declarations and Access Controls Part 3</title><content type="html">In Declarations and Access Controls Part 2, I discussed what it means to be a legal identifier and what is widely accepted in practice. Again, remember that an identifier does not need to conform to conventions to be legal. Take a quick look of the rules &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_05.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-weight: bold;"&gt;Class Declaration and Access Modifiers&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In creating a Java application you will mostly create classes and interfaces and how you write them will heavily affect your whole application. In this part I will discuss how we declare classes and how you can modify access to them.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Source File Declaration Rules&lt;/span&gt;. Before going to declaring classes let's first take a look at the rules in naming your source files, defining packages and using import statements. In the last section of this series we will go deeper into import statements and packages but if you want to know more about them you can go to &lt;a href="http://java.sun.com/docs/books/tutorial/java/package/packages.html"&gt;Sun's Java Package Tutorial&lt;/a&gt;.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;You can have multiple classes in a source file but only one &lt;code&gt;public&lt;/code&gt; class is allowed per source file.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;If your source file has a public class, the name of the source file should be the same as the public class' name (e.g. a class declared as &lt;code&gt;public class Person&lt;/code&gt; should be in a source code file &lt;code&gt;Person.java&lt;/code&gt;).&lt;/li&gt;&lt;li&gt;A source file can have multiple non-public classes.&lt;/li&gt;&lt;li&gt;A source file with no public class can have any filename.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;If the class is part of a package, the package declaration should be the first line in the source file.&lt;/li&gt;&lt;li&gt;If you are using classes from different packages in a source file then import statements for those classes should come next after the package declaration and before the class declaration. If the class is not part of a package the import statements should be the first lines in the source file. If there are no package declaration and import statements, the class declaration should appear on the first line in the source file.&lt;/li&gt;&lt;li&gt;Import statements and package declaration in a source file applies to all classes within the source file. You cannot declare different packages for each class in your source file.&lt;/li&gt;&lt;li&gt;Lastly, comments may appear anywhere in the source file. They are exempted from the rules mentioned above.&lt;/li&gt;&lt;/ul&gt;Example: &lt;pre class="brush: java"&gt;package com.blogspot.bobhub.scjp;&lt;br /&gt;&lt;br /&gt;import java.util.*;&lt;br /&gt;import javax.swing.*;&lt;br /&gt;&lt;br /&gt;public class Person{}&lt;br /&gt;class Employee{}&lt;/pre&gt;This should be saved in &lt;code&gt;Person.java&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Class Declaration.&lt;/span&gt; Nested classes will be discussed in future posts. The following code is a basic class declaration:&lt;br /&gt;&lt;pre id="class1" class="brush: java"&gt;class Person{}&lt;/pre&gt;Classes are declared using the &lt;code&gt;class&lt;/code&gt; keyword followed by the name of the class. The opening brace(&lt;code&gt;{&lt;/code&gt;) signifies the beginning of the class and the close brace (&lt;code&gt;}&lt;/code&gt;) ends the class. The code above compiles and you'll learn that it's the minimum requirement for declaring a class (If you have doubts try it. For a quick help on compiling your .java files go &lt;a href="http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html"&gt;here&lt;/a&gt;). In addition to the basic class declaration you can add modifiers. We will discuss class access modifiers first and non-access modifiers next.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Class Access Modifiers.&lt;/span&gt; Access modifiers restrict or allow usage of a class. Technically there are 4 access levels but there are only 3 access modifiers (&lt;code&gt;public&lt;/code&gt;, &lt;code&gt;private&lt;/code&gt; and &lt;code&gt;protected&lt;/code&gt;), the last one is called default or package access which you get when you don't specify any access modifier like our first class declaration&lt;a href="#class1"&gt; example&lt;/a&gt;. Access modifiers are also used with methods and instance variables.&lt;br /&gt;&lt;br /&gt;Although Java has 4 access levels a class can only be declared with a &lt;code&gt;public&lt;/code&gt; or default access.&lt;br /&gt;&lt;br /&gt;Access control for classes dictates what a class can do with another class. Let's say we have two classes, class X and class Y. When we say that class X has access to class Y it means class X can do one or more of these:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Create an instance of class Y.&lt;pre class="brush: java"&gt;class Y{}&lt;br /&gt;class X{&lt;br /&gt;  public Y y = new Y(); //instance variable&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Extend class Y. &lt;pre class="brush: java"&gt;class Y{}&lt;br /&gt;class X extends Y{}&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Access certain variables and methods within class Y, depending on the access control of those variables and methods.&lt;pre class="brush: java"&gt;class Y{&lt;br /&gt;  public int value; //instance variable&lt;br /&gt;}&lt;br /&gt;class X{&lt;br /&gt;  public static void main(String[] args){&lt;br /&gt;    Y y = new Y();&lt;br /&gt;    y.value = 24; //access "value"&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;Access level is sometimes called &lt;span style="font-style: italic;"&gt;visibility&lt;/span&gt;. Access modifiers and non-access modifiers are placed anywhere before the class keyword but you will notice that access modifiers usually are placed before non-access modifiers. Suffice to say, a class can only have one access modifier at a time.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Public Access.&lt;/span&gt; If a class is declared with a &lt;code&gt;public&lt;/code&gt; keyword then all of the classes in any package can access that class. Just don't forget that you still need to import the public class if you're going to use it in a class on a different package.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;package bobhub;&lt;br /&gt;public class Person{}&lt;/pre&gt;&lt;pre class="brush: java"&gt;package bobhub;&lt;br /&gt;public class Employee extends Person{}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Default Access.&lt;/span&gt; A class with no access modifier is said to have default or package-level access. This means that only classes within the same package as the declared class has access to it.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: java"&gt;package bobhub;&lt;br /&gt;class Employee{}&lt;/pre&gt;&lt;pre class="brush: java"&gt;package bobhub;&lt;br /&gt;public class TestEmployee{&lt;br /&gt;  public static void main(String[] args){&lt;br /&gt;      Employee employee = new Employee();&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;Class Employee and class TestEmployee will compile. But what if class TestEmployee is declared like this:&lt;pre class="brush: java"&gt;package scjp;&lt;br /&gt;&lt;br /&gt;import bobhub.Employee;&lt;br /&gt;&lt;br /&gt;public class TestEmployee{&lt;br /&gt;  public static void main(String[] args){&lt;br /&gt;      Employee employee = new Employee();&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;Try this on your own and take note of the result.&lt;br /&gt;&lt;br /&gt;That ends this post. The next post will be about &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_08.html"&gt;non-access modifiers&lt;/a&gt; we can use with classes. If you have something to share feel free to comment. Discussion is another way to study exams like the SCJP.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-1767540971279258036?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/P69B06Cv0unioTYoG1__iAbuhWE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/P69B06Cv0unioTYoG1__iAbuhWE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/P69B06Cv0unioTYoG1__iAbuhWE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/P69B06Cv0unioTYoG1__iAbuhWE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/7VbfLRw2f2g" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/1767540971279258036/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_06.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/1767540971279258036?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/1767540971279258036?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/7VbfLRw2f2g/scjp-16-study-guide-declarations-and_06.html" title="SCJP 1.6 Study Guide: Declarations and Access Controls Part 3" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_06.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0MDQ3k4fyp7ImA9WxNUFEg.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-7235881719468631227</id><published>2009-11-05T01:19:00.017+08:00</published><updated>2009-11-06T06:17:52.737+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-06T06:17:52.737+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="JavaBean Standards" /><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="SCJP 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java Keywords" /><category scheme="http://www.blogger.com/atom/ns#" term="Java Naming Convention" /><category scheme="http://www.blogger.com/atom/ns#" term="Java Identifiers" /><title>SCJP 1.6 Study Guide: Declarations and Access Controls Part 2</title><content type="html">This is Part 2 of Declarations and Access Controls Section. &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and.html"&gt;Part 1&lt;/a&gt; was just a refresher if you have been away from Java for long. I will discuss legal identifiers, keywords, Sun/Java naming conventions, and the JavaBean naming convention.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-weight: bold;"&gt;Identifiers and JavaBeans&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In Java, we will be creating lots of classes with variables and methods. As such, we need to be able to identify each one to be able to use them, same as us having names to call each other. We call these names as identifiers and Java has rules on creating them.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Legal Identifiers.&lt;/span&gt; Legal identifiers are identifiers which comply to rules in creating identifiers set by Java.&lt;br /&gt;&lt;br /&gt;Rules for Legal Identifiers:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Must start ONLY with a letter, a dollar sign ($), or an underscore (_).&lt;/li&gt;&lt;li&gt;After the first character, the identifier can contain any combination of letters, numbers, dollar signs and underscores.&lt;/li&gt;&lt;li&gt;Must not be one of the &lt;a href="http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html"&gt;Java keywords&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Identifiers are case-sensitive. So &lt;code&gt;bar&lt;/code&gt; is different from &lt;code&gt;Bar&lt;/code&gt; (Not exactly a rule but you need to remember this when you are using identifiers)&lt;/li&gt;&lt;/ol&gt;Examples of LEGAL identifiers:&lt;br /&gt;&lt;pre class="brush: java"&gt;int buzz;&lt;br /&gt;int $name;&lt;br /&gt;int _AgE;&lt;br /&gt;int $$_$;&lt;br /&gt;int an$unnecessarily_long$identifier_;&lt;br /&gt;&lt;/pre&gt;Examples of ILLEGAL identifiers:&lt;br /&gt;&lt;pre class="brush: java"&gt;double 8uZz;&lt;br /&gt;double !add;&lt;br /&gt;double e-mail;&lt;br /&gt;double %xyz;&lt;br /&gt;double Abc def;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;" id="keywords"&gt;Keywords.&lt;/span&gt; Keywords are reserved words by Java. These are primarily used as syntax in Java. Below is a table of Java keywords. Don't worry if you are not yet familiar with some of them we will tackle them as we go along. Click &lt;a href="http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html"&gt;here&lt;/a&gt; to see the table.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Java Code Convention.&lt;/span&gt; Using a set of code standards helps in reducing effort in testing, maintaining and enhancing existing codes.&lt;br /&gt;&lt;br /&gt;Remember that even if an identifier does not follow the naming convention if it adheres to the rules of a legal identifier your code will still compile.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Classes and Interfaces.&lt;/span&gt; First letter should be capitalized and if the identifier is composed of several words then the first letter of each subsequent word should be capitalized as well.&lt;br /&gt;&lt;br /&gt;Class names should be nouns.&lt;br /&gt;Examples:&lt;br /&gt;&lt;pre class="brush: java"&gt;Person&lt;br /&gt;DepartmentHead&lt;br /&gt;MyPetStore&lt;br /&gt;&lt;/pre&gt;Interface names should be adjectives.&lt;br /&gt;Examples:&lt;br /&gt;&lt;pre class="brush: java"&gt;Serializable&lt;br /&gt;Comparable&lt;br /&gt;Runnable&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-style: italic;"&gt;Methods.&lt;/span&gt; Should follow camelCase convention. First letter of first word is lowercase and the first letter of the succeeding words should be capitalized. Method names typically are verb-noun compounds.&lt;br /&gt;&lt;br /&gt;Examples:&lt;br /&gt;&lt;pre class="brush: java"&gt;drawShape&lt;br /&gt;createPlayer&lt;br /&gt;interpolateValue&lt;/pre&gt;&lt;span style="font-style: italic;"&gt;Variables.&lt;/span&gt; Should follow camelCase convention as well. Variable names are usually nouns. Make them short but descriptive.&lt;br /&gt;&lt;br /&gt;Examples:&lt;br /&gt;&lt;pre class="brush: java"&gt;firstName&lt;br /&gt;candidateList&lt;br /&gt;result&lt;/pre&gt;&lt;span style="font-style: italic;"&gt;Constants.&lt;/span&gt; Java constants are created by using the keywords &lt;code&gt;static&lt;/code&gt; and &lt;code&gt;final&lt;/code&gt;. All letters are capitalized and words are separated by underscores.&lt;br /&gt;&lt;br /&gt;Examples:&lt;br /&gt;&lt;pre class="brush: java"&gt;BUTTON_WIDTH&lt;br /&gt;STEEL_GAGE&lt;br /&gt;YOUNGS_MODULO&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;JavaBeans Standards.&lt;/span&gt; JavaBeans are classes with properties which usually are private instance variables. Private properties can be accessed by other class only through &lt;span style="font-style: italic;"&gt;getters&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;setters&lt;/span&gt;. Getters are methods which retrieve a property's value and setters are methods which change a property's value.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;If the property is not boolean then the method name's prefix should be get (e.g. &lt;code&gt;getName()&lt;/code&gt;).&lt;/li&gt;&lt;li&gt;If the property is boolean the method name's prefix may either be get or is (e.g. &lt;code&gt;isValid()&lt;/code&gt;).&lt;/li&gt;&lt;li&gt;The setter method prefix should be set (e.g. setName()).&lt;/li&gt;&lt;li&gt;To complete the method name just capitalize the first letter of the property's name and append it to the prefix. (e.g. property name: &lt;code&gt;account&lt;/code&gt;, getter: &lt;code&gt;getAccount()&lt;/code&gt;, setter: &lt;code&gt;setAccount()&lt;/code&gt;).&lt;/li&gt;&lt;li&gt;Setters should be marked public, takes an argument of the property's type and returns void.&lt;/li&gt;&lt;li&gt;Getters should be marked public, takes no argument and should return a value of the property's type.&lt;/li&gt;&lt;/ul&gt;Example&lt;br /&gt;&lt;pre class="brush: java"&gt;public class Person{&lt;br /&gt;   private int age;&lt;br /&gt;&lt;br /&gt;   public int getAge(){&lt;br /&gt;       return age;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void setAge(int value){&lt;br /&gt;       age = value;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Events.&lt;/span&gt; "&lt;span style="font-style: italic;"&gt;The JavaBean spec supports events which allow components to notify each other that something happened (from SCJP 1.6 Study Guide book)&lt;/span&gt;". Events are used primarily with GUI applications.&lt;br /&gt;&lt;br /&gt;The object which receives the event object is called the &lt;span style="font-style: italic;"&gt;listener&lt;/span&gt;. Like private instance variables, listeners are added and removed by methods which may follow these conventions:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Methods which add/register a listener to a component should be prefixed with "&lt;span style="font-style: italic;"&gt;add&lt;/span&gt;" (e.g. &lt;code&gt;addMouseListener()&lt;/code&gt;).&lt;/li&gt;&lt;li&gt;Methods which remove/unregister listeners from a component should be prefixed with "&lt;span style="font-style: italic;"&gt;remove&lt;/span&gt;" (e.g. &lt;code&gt;removeMouseListener()&lt;/code&gt;).&lt;/li&gt;&lt;li&gt;Register and unregister methods should always end with "&lt;span style="font-style: italic;"&gt;Listener&lt;/span&gt;".&lt;br /&gt;&lt;/li&gt;&lt;li&gt;addXxxListener and removeXxxListener methods should take an argument with the type of listener.&lt;/li&gt;&lt;/ul&gt;Examples of VALID JavaBean method signatures:&lt;br /&gt;&lt;pre class="brush: java"&gt;public void setBalance(double balance)&lt;br /&gt;public double getBalance()&lt;br /&gt;public boolean isEnabled()&lt;br /&gt;public void addWindowListener(WindowListener w)&lt;br /&gt;public void removeWindowListener(WindowListener w)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Examples of INVALID JavaBean method signatures:&lt;br /&gt;&lt;pre class="brush: java"&gt;&lt;br /&gt;//should be public&lt;br /&gt;private void setBalance(double balance)&lt;br /&gt;//should return a value of the property's type              &lt;br /&gt;public void getBalance()&lt;br /&gt;//should start with either set or is                            &lt;br /&gt;public boolean determineIfSuccessful()&lt;br /&gt;//listener type mismatch              &lt;br /&gt;public void addWindowListener(KeyListener k)&lt;br /&gt;//listener type mismatch        &lt;br /&gt;public void removeActionListener(WindowListener w)&lt;/pre&gt;&lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_06.html"&gt;Part 3&lt;/a&gt; of the Declarations and Access Controls section will discuss about Class Declaration and Modifiers.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-7235881719468631227?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/xaAMnIEUUbXsOM1BqqupiSltRGw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xaAMnIEUUbXsOM1BqqupiSltRGw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/xaAMnIEUUbXsOM1BqqupiSltRGw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xaAMnIEUUbXsOM1BqqupiSltRGw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/3GaQNqQFFRU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/7235881719468631227/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_05.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/7235881719468631227?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/7235881719468631227?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/3GaQNqQFFRU/scjp-16-study-guide-declarations-and_05.html" title="SCJP 1.6 Study Guide: Declarations and Access Controls Part 2" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_05.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0EEQ3c5eyp7ImA9WxNUGEs.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-3450926601109986492</id><published>2009-11-04T17:04:00.010+08:00</published><updated>2009-11-11T00:13:22.923+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-11T00:13:22.923+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="SCJP 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer 1.6" /><title>SCJP 1.6 Study Guide: Declarations and Access Controls Part 1</title><content type="html">&lt;span style="font-weight: bold;font-size:130%;" &gt;Objectives:&lt;/span&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Declare classes and interfaces (Normal class, abstract class and interface)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Use primitives, arrays, enums and legal identifiers&lt;/li&gt;&lt;li&gt;Use static methods, JavaBeans naming and var-args&lt;/li&gt;&lt;/ol&gt;As I've said on the parent post this is a study guide for the SCJP 1.6 Exam so I assume you have at least some knowledge of Java already. But here's a refresher:&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;Refresher&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Class&lt;/span&gt; - A blueprint that defines the state and behavior for an object.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Object&lt;/span&gt; - Instance of a class with its own state and access to behaviors defined in its class. Usually created using the &lt;span style="font-weight: bold;"&gt;new&lt;/span&gt; operator.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;State&lt;/span&gt; - Instance variables of a class.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Behavior&lt;/span&gt; - Methods of the class.&lt;br /&gt;&lt;br /&gt;&lt;span id="ex11"&gt;Example 1.1:&lt;/span&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;public class Person{&lt;br /&gt;  public String name;&lt;br /&gt;  public int age;&lt;br /&gt;&lt;br /&gt;  public void eat(){&lt;br /&gt;    System.out.println("Eating...");&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class TestPerson{&lt;br /&gt;  public static void main(String[] args){&lt;br /&gt;    Person p = new Person()&lt;br /&gt;    p.name = "John Doe";&lt;br /&gt;    p.age = 24;&lt;br /&gt;    p.eat();&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;In the example we have a Person class with instance variables name and age and an eat() method. When we created a Person object (&lt;code&gt;Person p = new Person&lt;/code&gt;) we gave it a name and an age (John Doe, 24) which defines the state of object p. We also can use the eat() method since the p object can access it.&lt;br /&gt;&lt;br /&gt;Most Java programs will be composed of objects created from different classes communicating with each other.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Identifiers and Keywords&lt;/span&gt;. All &lt;span style="font-weight: bold;"&gt;&lt;/span&gt;classes, variables and methods that we will create need names and we call them &lt;span style="font-style: italic;"&gt;identifiers&lt;/span&gt; and there are rules on how we create legal identifiers.&lt;br /&gt;&lt;br /&gt;Moreover, since the creators and developers of Java knew that it will be used by a large community they developed conventions in creating identifiers so we could understand each others code.&lt;br /&gt;&lt;br /&gt;Remember that for our codes to compile we only need our identifiers to be legal. But when you are developing your application it is highly recommended that we follow the conventions.&lt;br /&gt;&lt;br /&gt;In &lt;a href="http://www.blogger.com/post-create.g?blogID=8330726088285586398#ex11"&gt;Example 1.1&lt;/a&gt; class Person defined these identifiers:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Person&lt;/li&gt;&lt;li&gt;name&lt;/li&gt;&lt;li&gt;age&lt;/li&gt;&lt;li&gt;eat&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;span style="font-weight: bold;"&gt;Inheritance&lt;/span&gt;. Inheritance is one of the concepts of Object Orientation. It allows one class to be reused or extended by another class.&lt;br /&gt;&lt;br /&gt;A class that is extended by another class is called the &lt;span style="font-style: italic;"&gt;super class&lt;/span&gt; and the class extending the super class is called a &lt;span style="font-style: italic;"&gt;subclass&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;A subclass automatically inherits accessible instance variables and methods of the super class.&lt;br /&gt;&lt;br /&gt;A subclass may also override methods it inherits from the super class.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Interfaces&lt;/span&gt;. Interfaces are 100% abstract classes. Classes implement interfaces instead of extending.&lt;br /&gt;&lt;br /&gt;An interface defines methods that an implementing class should have but does not dictate how those methods should be written. The implementing class should define the actual code for those methods.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Packages&lt;/span&gt;. A typical Java application will usually use many classes. As such it is best to group related classes into packages or folders to give developers a consistent way of naming, accessing different classes and prevent naming conflicts.&lt;br /&gt;&lt;br /&gt;That's it for this post. &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_05.html"&gt;Part 2&lt;/a&gt; will tackle Identifiers and JavaBeans.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-3450926601109986492?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ZM5Y5mRHCgkPuoc47VBeIhQByPw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ZM5Y5mRHCgkPuoc47VBeIhQByPw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ZM5Y5mRHCgkPuoc47VBeIhQByPw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ZM5Y5mRHCgkPuoc47VBeIhQByPw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/4FZGvg00qrg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/3450926601109986492/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/3450926601109986492?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/3450926601109986492?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/4FZGvg00qrg/scjp-16-study-guide-declarations-and.html" title="SCJP 1.6 Study Guide: Declarations and Access Controls Part 1" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEIGRHc-fyp7ImA9WxNbFkg.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-7043020497640215124</id><published>2009-11-04T16:39:00.015+08:00</published><updated>2009-11-20T02:48:45.957+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-20T02:48:45.957+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer" /><category scheme="http://www.blogger.com/atom/ns#" term="SCJP 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java 1.6" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Sun Certified Java Programmer 1.6" /><title>SCJP 6 Study Guide</title><content type="html">Since I'm reviewing for the SCJP 1.6 Exam (Exam 310-065) I thought writing about the topics I study will help me remember them better. So this series of posts is basically based on what I'll read from the  &lt;span class="pagetitle"&gt;&lt;a href="http://www.amazon.com/SCJP-Certified-Programmer-Java-310-065/dp/0071591060"&gt;SCJP Sun® Certified Programmer for Java™ 6 Study Guide&lt;/a&gt; book by Kathy Sierra and Bert Bates and things I pick up from other sources which I will mention accordingly.&lt;br /&gt;&lt;br /&gt;This series can also be a reference for people starting to learn Java but I recommend checking &lt;a href="http://java.sun.com/docs/books/tutorial/getStarted/index.html"&gt;Sun's Java Tutorials&lt;/a&gt;.&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;Here are the topics I'll cover:&lt;br /&gt;&lt;/span&gt;&lt;ol&gt;&lt;li&gt;Declaration and access control - &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and.html"&gt;1&lt;/a&gt;|&lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_05.html"&gt;2&lt;/a&gt;|&lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_06.html"&gt;3&lt;/a&gt;|&lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_08.html"&gt;4&lt;/a&gt;|&lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_8808.html"&gt;5&lt;/a&gt;|&lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_09.html"&gt;6&lt;/a&gt;|&lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_12.html"&gt;7&lt;/a&gt;|&lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_13.html"&gt;8&lt;/a&gt;|&lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-declarations-and_16.html"&gt;9&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Object orientation - &lt;a href="http://bobhub.blogspot.com/2009/11/scjp-16-study-guide-object-orientation.html"&gt;1&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Assignment&lt;/li&gt;&lt;li&gt;Operators&lt;/li&gt;&lt;li&gt;Flow control&lt;/li&gt;&lt;li&gt;Exceptions&lt;/li&gt;&lt;li&gt;Assertions&lt;/li&gt;&lt;li&gt;String IO, formatting and parsing&lt;/li&gt;&lt;li&gt;Generics and collections&lt;/li&gt;&lt;li&gt;Inner classes&lt;/li&gt;&lt;li&gt;Threads&lt;/li&gt;&lt;li&gt;Development&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;span class="pagetitle"&gt;For a detailed description of the exam objectives go to &lt;a href="http://www.sun.com/training/catalog/courses/CX-310-065.xml"&gt;SCJP 1.6 Objectives&lt;/a&gt;. If a topic is ready it will appear on the list above as a link. Hope you can discuss with me on each one. Please don't ask me for a copy of the book, go buy one and help the authors and the people who contributed to help us prepare for the exam.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-7043020497640215124?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/_w9Vuu_VpSJLx3PmTC_ENVszai0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_w9Vuu_VpSJLx3PmTC_ENVszai0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/_w9Vuu_VpSJLx3PmTC_ENVszai0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_w9Vuu_VpSJLx3PmTC_ENVszai0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/Gi8-gUTicwQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/7043020497640215124/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/11/scjp-6-study-guide.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/7043020497640215124?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/7043020497640215124?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/Gi8-gUTicwQ/scjp-6-study-guide.html" title="SCJP 6 Study Guide" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>1</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/11/scjp-6-study-guide.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0UBRH0yfCp7ImA9WxNVGEQ.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-4535001057246367404</id><published>2009-10-30T16:13:00.005+08:00</published><updated>2009-10-30T16:27:35.394+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-30T16:27:35.394+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="MySQL" /><category scheme="http://www.blogger.com/atom/ns#" term="slow connection" /><category scheme="http://www.blogger.com/atom/ns#" term="skip name resolve" /><title>Slow MySQL Remote Connection</title><content type="html">If your application is connecting very slow to a remote MySQL database you might want to try adding this line to the my.ini file of the server:&lt;br /&gt;&lt;br /&gt;skip-name-resolve&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;Basically, when a new client connects to MySQL, it&lt;span style="text-decoration: underline;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysqld.html" title="4.3.1. mysqld — The MySQL Server"&gt;&lt;span&gt;&lt;strong class="command"&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/a&gt; spawns a new thread to handle the         request. This thread first checks whether the host name is in         the host name cache. If not, the thread attempts to resolve the         host name. This may take a lot of time to finish so to ignore this process we add the above line to my.ini file.&lt;br /&gt;&lt;br /&gt;Please see &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/dns.html"&gt;MySQL documentation&lt;/a&gt; for further explanation.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-4535001057246367404?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/pFAgf-YIE1oMERWD7w6gwMAxd5I/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/pFAgf-YIE1oMERWD7w6gwMAxd5I/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/pFAgf-YIE1oMERWD7w6gwMAxd5I/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/pFAgf-YIE1oMERWD7w6gwMAxd5I/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/7SANJLqLs0Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/4535001057246367404/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/10/slow-mysql-remote-connection.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/4535001057246367404?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/4535001057246367404?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/7SANJLqLs0Y/slow-mysql-remote-connection.html" title="Slow MySQL Remote Connection" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/10/slow-mysql-remote-connection.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ck8CRX49eip7ImA9WxNVGEQ.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-4050616340570057598</id><published>2009-10-13T20:57:00.008+08:00</published><updated>2009-10-30T16:21:04.062+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-30T16:21:04.062+08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="epson tm 220" /><category scheme="http://www.blogger.com/atom/ns#" term="receipt printer" /><category scheme="http://www.blogger.com/atom/ns#" term="hardware" /><category scheme="http://www.blogger.com/atom/ns#" term="epson" /><category scheme="http://www.blogger.com/atom/ns#" term="epson tm 220 usb" /><title>Installing an Epson TM U220 using USB in Windows XP</title><content type="html">Today I was tasked to go to one of our clients in Makati to install a receipt printer (Epson TM U220) and use a printer port-usb connector because the CPU there does not have any parallel port (I think almost all modern CPUs do not have parallel ports already, at least my laptop doesn't have one).&lt;br /&gt;&lt;br /&gt;I was looking for the CD installer of the printer but could not find it. Fortunately, after 30 minutes of sniffing the web I was able to find one. So for those who need it go to &lt;a href="http://install.westgatesoftware.com/drivers/PRINTERS/Epson/TM-T90/"&gt;http://install.westgatesoftware.com/drivers/PRINTERS/Epson/TM-T90/&lt;/a&gt; and download APD304e.exe or just search for that executable file using &lt;a href="http://www.google.com.ph/search?q=apd304e.exe&amp;amp;ie=utf-8&amp;amp;oe=utf-8&amp;amp;aq=t&amp;amp;rls=org.mozilla:en-US:official&amp;amp;client=firefox-a"&gt;Google&lt;/a&gt;. Just follow these steps to install the driver:&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;ol style="text-align: left;"&gt;&lt;li&gt;Run the installer.&lt;/li&gt;&lt;li&gt;Select the right OS.&lt;/li&gt;&lt;li&gt;Select USB as the port to be used.&lt;/li&gt;&lt;li&gt;Select the correct model from the list.&lt;/li&gt;&lt;li&gt;Let the installer do its work.&lt;/li&gt;&lt;li&gt;Restart your computer.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;The other hard part is, if you're not familiar with installing printers using USB port instead of a printer port without the CD installer which I am earlier), how do I make it work after running the installer. Here's a simple procedure:&lt;br /&gt;&lt;ol style="text-align: left;"&gt;&lt;li&gt;Go to Control Panel and select Printer and Faxes.&lt;/li&gt;&lt;li&gt;Select the correct printer from the list, in this case it's Epson TM U220, right click and select Properties.&lt;/li&gt;&lt;li&gt;Go to the Port tab.&lt;/li&gt;&lt;li&gt;You'll see that the printer you've just installed is using USB. You have to change that. Look for `Virtual Printer Port for USB` and select it and Apply.&lt;/li&gt;&lt;li&gt;If you check the Printer and Faxes folder again, the status of the printer should be `Ready`. If it's not just restart your computer.&lt;/li&gt;&lt;li&gt;After restarting, go right back to the Properties of the printer and go to the Port tab and uncheck both `Enable bidirectional support` and `Enable printer pooling`.&lt;/li&gt;&lt;li&gt;Run a Test Print.&lt;/li&gt;&lt;li&gt;If it did not print then go to &lt;a href="http://cookingprogrammers.blogspot.com/2008/09/installing-epson-tm-printer-in-windows.html"&gt;Code and Cook&lt;/a&gt; where I got most of this (look at the comments maybe those can help you) and what made me write about it again because I thought it is a helpful article.&lt;/li&gt;&lt;/ol&gt;Good luck and share your experience.&lt;br /&gt;&lt;br /&gt;- Bob&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-4050616340570057598?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/OGH1t9jAfw5JA8bUN3yJ-h5ynuk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OGH1t9jAfw5JA8bUN3yJ-h5ynuk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/OGH1t9jAfw5JA8bUN3yJ-h5ynuk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OGH1t9jAfw5JA8bUN3yJ-h5ynuk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/mcM3mSorqyk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/4050616340570057598/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/10/installing-epson-tm-u220-using-usb-in.html#comment-form" title="9 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/4050616340570057598?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/4050616340570057598?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/mcM3mSorqyk/installing-epson-tm-u220-using-usb-in.html" title="Installing an Epson TM U220 using USB in Windows XP" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>9</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/10/installing-epson-tm-u220-using-usb-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ck4FSH8-eip7ImA9WxNWFEg.&quot;"><id>tag:blogger.com,1999:blog-8330726088285586398.post-5457961912118319717</id><published>2009-10-12T23:22:00.006+08:00</published><updated>2009-10-14T00:21:59.152+08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-14T00:21:59.152+08:00</app:edited><title>Blogging at Blogger</title><content type="html">&lt;div style="text-align: left;"&gt;Hi all! I'm Bob Santos and this blog will be my personal repository of things I find worth writing. Topics may range from programming to gaming, from music to movies and from humor to politics.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8330726088285586398-5457961912118319717?l=bobhub.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/8QkLeD_K0syppH0lVv9wTzHAOho/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8QkLeD_K0syppH0lVv9wTzHAOho/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/8QkLeD_K0syppH0lVv9wTzHAOho/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8QkLeD_K0syppH0lVv9wTzHAOho/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Bobhub/~4/3fZWHpGykLQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bobhub.blogspot.com/feeds/5457961912118319717/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://bobhub.blogspot.com/2009/10/blogging-at-blogger.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/5457961912118319717?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8330726088285586398/posts/default/5457961912118319717?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Bobhub/~3/3fZWHpGykLQ/blogging-at-blogger.html" title="Blogging at Blogger" /><author><name>Bob</name><uri>http://www.blogger.com/profile/14068388959313667128</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="26" height="32" src="http://4.bp.blogspot.com/_gJiv-rYtBSI/SvF-UQGXnuI/AAAAAAAAAAM/XZy2mj_bdYY/S220/avatar1.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://bobhub.blogspot.com/2009/10/blogging-at-blogger.html</feedburner:origLink></entry></feed>

