Understanding The Java Class Structure: Comments

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

 

OCA Java SE 8 Study Group 
1. Java Basics ­ Handout 
 
 

Understanding the Java Class Structure 
● Each  Java  class  has the following signature: [modifiers] class classIdentifier [extends 
superClassIdentifier] [implements interfaceIdentifier1, interfaceIdentifier2, etc.] 
● A class may extend one and only one superclass. 
● A class may implement one or more interfaces. 
● Interfaces are separated by commas. 
● The  preferred  order  of  presenting  elements  in  a  class  is  data  members,  followed  by  
constructors, followed by methods. 

Comments 
● Comments  are  non­executable  sections  which  explain  the  actions  of  a  particular 
piece of code. 
● Comments  can be placed anywhere in the source code. 
● There are three types of comments in Java: 
○ Single line comment ­ // and everything after that on the same line is ignored. 
○ Multi­line comment ­ /*...*/ and everything between them is ignored. 
○ Javadoc  comment  ­  /**...*/  is used for documentation and everything between 
them is ignored. 

Classes VS. Files 
● A source code file can have only one public class. 
● If  the   source   file  contains  a  public  class,  the  file  name  must  match  the  public  class 
name. 
● A file can have more than one non­public class. 
● Files with no public classes have no naming restrictions. 

Naming Conventions 
● Naming   conventions  are  used  to  make  Java  programs  more  readable  and 
maintainable. 
● The following naming convention is applied to Java elements: 
○ Class  name/Interface  name/Enumeration  name  ­  begins  uppercase, 
continues camel case 
○ Method  name/instance  and  static  variable  names/parameters  and  local 
variable names ­ begins lowercase, continues camel case 
○ Generic type parameters ­ single uppercase letter 
○ Constants/Enumeration  values   ­  all  uppercase   letters,  multiple  words 
separated by underscores 
○ Package ­ all lowercase letters 

Compile and Interpret Java Code 
● A Java program begins execution with its main() method. 
● Legal main method signature: 


 
OCA Java SE 8 Study Group 
1. Java Basics ­ Handout 
 
 
○ public static void main(String a[]) 
○ public static void main(String[] a) 
○ public static void main(String... a) 

Compile Java Code 
● The Java compiler is invoked with the javac command. 
● To compile Java code, the file must have the extension .java. 
● The  compiler’s  ­d  command­line  option  defines  where  compiled class files  should be 
placed. 
● The  compiler’s  ­d  command­line  option  will  include  the  package  location  if  the  class 
has been declared with a package statement. 
● The  compiler’s  ­classpath  (or  ­cp)  command­line  option  defines  directory  paths  in 
search of classes. 
● The  classpath  option  is  optional  if  the  classpath  is  defined  in  the  CLASSPATH 
environment variable or the desired files are in the working directory. 
● On   Windows  systems  paths  are  delimited  with   semicolons  while  on   POSIX­based 
systems with colons. 
● The compiler also compiles dependent classes from the classpath if needed. 
● The compiler can compile classes not being in their correct package directory. 

Interpret Java Code 
● The Java interpreter is invoked with the java command. 
● Java bytecode files have the extension .class. 
● The interpreter’s ­classpath (or ­cp) switch defines directory paths to use at runtime. 
● The  javaw  command  on  Windows  can  be  used   to  start  a  program  without  the 
command window (similarly, using the ampersand on POSIX­based systems). 
● The  interpreter’s  ­D  command­line  option  allows  for  the  setting  of  system  property 
values. 
● The interpreter’s syntax for the ­D command­line option is ­Dproperty=value. 
● The  interpreter’s  ­version  command­line  option  is  used  to  return  the  version  of  the 
JVM and exit. 
● The interpreter does not run classes not being in their correct package directory. 
● The  –h  command­line  option   can  be  applied  either  to  the  compiler  or  the  interpreter 
to print out the tools usage information. 

   


 
OCA Java SE 8 Study Group 
1. Java Basics ­ Handout 
 
 

Understanding Package Declarations 
● Packages are containers for classes. 
● A package statement also defines the path where classes will be stored. 
● A package statement uses periods for delimitation. 
● Package names should be lowercase and separated with underscores between 
words. 
● Standard  coding  convention  for  package  statements  reverses  the  domain  name  of 
the organization or group creating the package. 
● There can be zero or one package statement per source file. 
● The package statement (if any) must be the first non­comment line in the source file. 
● Package  names  beginning  with  java.*  (core  packages)  and  javax.*  (standard 
extensions) are reserved. 
● The benefits of packaging: 
○ decreases the likelihood of class name collision 
○ package dependencies are reduced with class/system coupling 
○ larger  packages  support  reusability  (too  large  packages  can  result  in   hardly 
maintainable code) 
○ smaller  packages  support  maintainability (upon focused  functionality changes 
may be limited to a single package) 

Understand Imports 
● An import statement is used to include source code from external classes. 
● An  import  statement  occurs  after  the  optional  package  statement  and  before  the 
class definition. 
● An import statement can define a specific class name to import (explicit import). 
● An  import statement can use an asterisk to include all classes within a given package 
(implicit import). 
● Each import statement can relate to one package only. 
● The java.lang package that houses the core language classes is imported by default. 
● Importing the same class twice doesn’t break the code (it’s a warning only). 
● Every  java  class  automatically  imports  the  classes  from java.lang  and from their own 
package. 
● A  class  from  the  default  package  cannot  be  accessed  from  a  class  in  any  other 
package. 
● Naming   collisions  occur  when  more  classes  with  the   same  name  are  imported  from 
different packages. 
● Naming  collisions  can be solved by using the fully qualified name of the classes or by 
importing  the  default  one  with  an  explicit  import  (which  has  precedence  over  the 
implicit import). 
● Static imports exist since Java SE 5. 
● Static imports allow importing of static members and enum constants. 


 
OCA Java SE 8 Study Group 
1. Java Basics ­ Handout 
 
 

Understand Variable Scope 
● A variable’s scope defines what parts of the code have access to that variable. 
● Local  variables  are  declared  inside  methods,  constructors  or   blocks.  The  block  of 
code in which a variable is declared determines the scope of the local variable. 
● Instance  variables  are  declared  in  the  class,  not  inside  methods.  They represent the  
state of the object and are in scope as long as their object lives. 
● Static  variables  are  declared  in the class, not inside methods. There is only one copy 
of  the  same  static  variable  and  all  instances  share  it.  They  are  in  scope  until  the 
program ends. 
● It  is  legal  to  declare  a  local  variable  with  the  same  name   as  an  instance  or  static 
variable; this is called “shadowing”. 
● After a variable goes out of scope it can no longer be used and its value is lost. 

Benefits of Java 
● Object Oriented 
○ Java has a programming language model organized around objects.  
○ Java allows functional programming within classes. 
● Encapsulation 
○ Encapsulation  is  the  concept  of  storing  similar  data  (state)  and  methods 
(behaviour) together in classes. 
● Platform Independent 
○ Java is an interpreted language because it is compiled to bytecode.  
○ The  concept  “write  once, run anywhere” applies for Java since bytecode does 
not depend on the operating system. 
● Robust 
○ Memory  management  is  done  automatically  through  garbage  collection  in 
Java. 
● Simple 
○ Java  was  intended  to  be  simpler  than   C++  (no  pointers,  no  operator  
overloading). 
● Secure 
○ Java code runs in the JVM (no exploits like out­of­bound arrays, pointers). 

You might also like