Contents - Cultural View
Contents - Cultural View Contents - Cultural View
Java syntax 188 Classes Classes are fundamentals of an object-oriented language such as Java. They contain members that store and manipulate data in real-life-like way. Classes are divided into top-level and nested. Nested classes include member classes, local classes and anonymous classes. Declaration Top-level class class Foo { // Class members } Member class class Foo { // Top-level class Local class class Foo { } class Bar { // Nested class } void bar() { Anonymous class class Foo { Initialization } } } class Foobar {// Local class within a method } void bar() { } new Object() {// Creation of a new anonymous class extending Object }; To use non-static members of the class it must be instantiated. It is done by using the new keyword and calling the class constructor. Foo foo = new Foo(); Accessing members Members of both instances and static classes are accessed with the . operator. Accessing an instance member Instance members can be accessed through the name of a variable. String foo = "Hello"; String bar = foo.toUpperCase(); Accessing a static class member Static members are accessed by using the name of the class or any other type. This does not require the creation of a class instance. Static members are declared using the static modifier. public class Foo { } public static void doSomething() { } // Calling the static method
Java syntax 189 Foo.doSomething(); Modifiers Modifiers are keywords used to modify declarations of types and type members. Most notably there is a sub-group containing the access modifiers. • abstract - Specifies that a class only serves as a base class and cannot be instantiated. • static - Used only for member classes, specifies that the member class does not belong to a specific instance of the containing class. • final - Classes marked as final cannot be extended from and cannot have any subclasses. • strictfp - Specifies that all floating-point operations must be carried out conforming to IEEE 754 and forbids using enhanced precision to store intermediate results. Access modifiers The access modifiers, or inheritance modifiers, set the accessibility of classes, methods, and other members. Members marked as public can be reached from anywhere. If a class or its member does not have any modifiers, default access is assumed. public class Foo { } int do() { } return 0; private class Bar { } The following table shows whether code within a class has access to the class or method depending on the accessing class location and the modifier for the accessed class or class member: Modifier Same class Other class inside the same package Subclass inside another package Non-subclass inside another private yes no no no default (package private) package yes yes no no protected yes yes yes no public yes yes yes yes Constructors A constructor is a special method that is called when an object is initialized. Its purpose is to initialize the members of the object. The main differences between constructors and ordinary methods are that constructors are named after the class and never return anything. They may take parameters as any common method. class Foo { } Foo() { } //Initialization
- Page 141 and 142: Head First (book series) 137 Head F
- Page 143 and 144: Inner class 139 • Local class - T
- Page 145 and 146: Interface (Java) 141 The body of th
- Page 147 and 148: Internet Foundation Classes 143 Int
- Page 149 and 150: Internet Foundation Classes 145 •
- Page 151 and 152: JAD (file format) 147 JAD (file for
- Page 153 and 154: JAMWiki 149 JAMWiki Developer(s) Ry
- Page 155 and 156: JGroups 151 JGroups Stable release
- Page 157 and 158: Java Class Library 153 Java Class L
- Page 159 and 160: Java Class Library 155 Alternative
- Page 161 and 162: Java Classloader 157 User-defined c
- Page 163 and 164: Java compiler 159 Java compiler A J
- Page 165 and 166: Java Development Kit 161 • jstack
- Page 167 and 168: Java Interface Definition Language
- Page 169 and 170: Java TV 165 See also • Xlet Exter
- Page 171 and 172: Java Work Framework 167 Database db
- Page 173 and 174: Java annotation 169 programmaticall
- Page 175 and 176: Java annotation 171 component.getAt
- Page 177 and 178: Java collections framework 173 Refe
- Page 179 and 180: Java syntax 175 Java syntax The syn
- Page 181 and 182: Java syntax 177 Double quote \" Tab
- Page 183 and 184: Java syntax 179 import statement Th
- Page 185 and 186: Java syntax 181 switch statement Sw
- Page 187 and 188: Java syntax 183 outer: for (int i =
- Page 189 and 190: Java syntax 185 The catch and final
- Page 191: Java syntax 187 Reference types Ref
- Page 195 and 196: Java syntax 191 Modifiers • abstr
- Page 197 and 198: Java syntax 193 } public void walk(
- Page 199 and 200: Java syntax 195 Inheritance Interfa
- Page 201 and 202: Java syntax 197 /* Any Mapper insta
- Page 203 and 204: Java: View Technologies and Framewo
- Page 205 and 206: JavaBeans Activation Framework 201
- Page 207 and 208: javac 203 References [1] http:/ / j
- Page 209 and 210: JavaFX 205 profile includes Swing a
- Page 211 and 212: JavaFX 207 External links • Offic
- Page 213 and 214: JExamples 209 JExamples Industry Co
- Page 215 and 216: Jikes 211 Jikes Developer(s) IBM St
- Page 217 and 218: Michael Kölling 213 Microsoft Pate
- Page 219 and 220: List of Java keywords 215 case catc
- Page 221 and 222: List of Java keywords 217 long nati
- Page 223 and 224: List of Java keywords 219 Reserved
- Page 225 and 226: Name mangling 221 Name mangling In
- Page 227 and 228: Name mangling 223 } }; }; /* = _ZN9
- Page 229 and 230: Name mangling 225 if (__1cGstrcmp6F
- Page 231 and 232: Name mangling 227 ['_Test__private_
- Page 233 and 234: Name mangling 229 External links
- Page 235 and 236: Patrick Naughton 231 See also • H
- Page 237 and 238: New I/O 233 FileChannel can be used
- Page 239 and 240: New I/O 235 // Pattern used to pars
- Page 241 and 242: New I/O 237 References [1] "JSR 51:
Java syntax 189<br />
Foo.doSomething();<br />
Modifiers<br />
Modifiers are keywords used to modify declarations of types and type members. Most notably there is a sub-group<br />
containing the access modifiers.<br />
• abstract - Specifies that a class only serves as a base class and cannot be instantiated.<br />
• static - Used only for member classes, specifies that the member class does not belong to a specific instance of<br />
the containing class.<br />
• final - Classes marked as final cannot be extended from and cannot have any subclasses.<br />
• strictfp - Specifies that all floating-point operations must be carried out conforming to IEEE 754 and forbids<br />
using enhanced precision to store intermediate results.<br />
Access modifiers<br />
The access modifiers, or inheritance modifiers, set the accessibility of classes, methods, and other members.<br />
Members marked as public can be reached from anywhere. If a class or its member does not have any modifiers,<br />
default access is assumed.<br />
public class Foo {<br />
}<br />
int do() {<br />
}<br />
return 0;<br />
private class Bar {<br />
}<br />
The following table shows whether code within a class has access to the class or method depending on the accessing<br />
class location and the modifier for the accessed class or class member:<br />
Modifier Same<br />
class<br />
Other class inside the same<br />
package<br />
Subclass inside another<br />
package<br />
Non-subclass inside another<br />
private yes no no no<br />
default (package<br />
private)<br />
package<br />
yes yes no no<br />
protected yes yes yes no<br />
public yes yes yes yes<br />
Constructors<br />
A constructor is a special method that is called when an object is initialized. Its purpose is to initialize the members<br />
of the object. The main differences between constructors and ordinary methods are that constructors are named after<br />
the class and never return anything. They may take parameters as any common method.<br />
class Foo {<br />
}<br />
Foo() {<br />
}<br />
//Initialization