@@ -3,6 +3,63 @@ layout: post
33title : Declarations
44---
55
6+ ### Class
7+
8+ * There can be __ only one public class__ per source code file.
9+ * If there is a public class in a file, the name of the file must match the name of the public class.
10+ * Files with no public classes can have a name that does not match any of the classes in the file.
11+ * A file can have __ more than one nonpublic class__ .
12+
13+
14+ ### Interface
15+
16+ * All __ interface methods are implicitly ` public ` and ` abstract ` __ . In other words, you do not need to actually type
17+ the ` public ` or ` abstract ` modifiers in the method declaration, but the method is still always ` public ` and ` abstract ` .
18+ * All __ variables defined in an interface are implicitly ` public ` , ` static ` , and ` final ` __ , in other words, interfaces can
19+ declare only constants, not instance variables.
20+ * Interface methods must not be ` static ` , ` final ` , ` strictfp ` , or ` native ` .
21+
22+ #### Some valid interface declarations:
23+
24+ {% highlight java %}
25+
26+ public abstract interface Rollable { } // abstract is redundant as
27+ // interfaces are implicitly abstract
28+
29+ public interface Rollable { } // public modifier is required if you
30+ // want the interface to have public
31+ // rather than default access
32+
33+ interface Rollable { } // interface with default access
34+
35+ public interface Bounceable {
36+
37+ // following initializations/declarations are all
38+ // legal and identical
39+
40+ int b = 8;
41+ public static final int b = 8;
42+ public final int b = 8;
43+ static final int b = 8;
44+ final int b = 8;
45+
46+ void bounce();
47+ public void bounce();
48+ abstract void bounce();
49+ public abstract void bounce();
50+ abstract public void bounce();
51+
52+ }
53+
54+ {% endhighlight %}
55+
56+
57+ _ NOTE: The above points state that interfaces have very little flexibility in how the methods and variables
58+ defined in the interface are declared. This is also one of the ** rare differences between an interface and an
59+ abstract class** ._
60+
61+
62+
663### Var-args
764
865Use var-args when you want to pass __ variable number of arguments__ to a method.
@@ -26,6 +83,7 @@ Let's look at some legal and illegal var-arg declarations:
2683 void doStuff6(String... s, byte b) { } // var-arg must be last
2784{% endhighlight %}
2885
86+
2987### Enums
3088
3189Java lets you restrict a variable to having one of only a few pre-defined values, in
@@ -173,4 +231,12 @@ enum CoffeeSize {
173231{% endhighlight %}
174232
175233
234+ __ Some points to note:__
235+
236+ * You can NEVER invoke an enum constructor directly. The enum constructor
237+ is invoked automatically, with the arguments you define after the constant value.
238+ * Every enum has a static method, values(), that returns an array of the enum's
239+ values in the order they're declared.
240+
241+
176242{% include responsive_ad.html %}
0 commit comments