Core Java

OCA Java 8 Preparation : Java Basics

Before we start with this series, please note that in this series of blog posts for OCA 8 preparation, I will be listing only those points which are important from the exam perspective .It will serve as a quick refresher before your exams and will help you score better .

For how to prepare for OCA 8 , refer this post.

With this, let’s get started. Today we’ll get a quick hold of Java Basics which are important for your OCA Java8 certification preparation.

General Guidelines :

  1. When included within double quotes, multi-line comments are treated as regular characters and not as comments.
  2. When we define a public class or an interface in a Java source file, the filename and the name of the public class or interface in the source file must match.Therefore it is not allowed to have multiple public classes or interfaces within the same Java source file.
  3. Classes and interfaces defined in the same source file can never belong to different java packages as Java allows only one package definition in the entire source file. Same is valid for import statements. Whatever classes/interfaces you import in a file are available to all the classes/interfaces in that source file.
  4. The order of the definition of instance variables, constructors and methods doesn’t matter at all in a class. A class may define an instance variable after a method definition and will still compile.
  5. It is valid to define a main() method by using any of the below signatures :
  6. It is possible to define multiple overloaded main methods within the class. However JVM will invoke exactly the one which matches its definition.
  7. The method parameters passed to the main method are called command-line parameters or command-line values. The index of parameters starts with 0 as JVM uses a String[] to pass command line values.
  8. Unlike C, Java doesn’t pass the name of the class as an argument to the main method.

Access Modifiers :

  1. Java provides four access levels – public,private, protected , default(or package-private). You can find more details on it at Oracle wensite here.
  2. A derived class can inherit and access protected members of its base class by inheritance , regardless of the package in which it’s defined.
  3. A derived class in a separate package can’t access protected members of its base class using reference variables .For eg : creating an object of base class in the child class and trying to access base class protected members in this derived class using this new object will fail to compile.
  4. Watch out for accessibility of a member based on its scope and access modifier.
  5. A outer class or an interface can only be defined using public or default access levels.A top level class or interface can’t be marked ‘static’
  6. Local variables or method parameters cannot be defined using an access modifier. The code will fail to compile.

Non-access modifiers(abstract, static, final) :

  1. The non-access modifiers you need to know for this exam includes : abstract, static and final.
  2. An abstract class can never be instantiated. Also don’t get tricked by a code which tries to mark a variable as abstract.That’s not a valid and code will fail to compile. Watch out for these in the exam.
  3. If a class has even a single abstract method, it must be defined abstract. However, the reverse is not true. An abstract class with no abstract methods will compile fine.
  4. The Java compiler automatically inserts abstract keyword in the interface definition.So, if you do it manually , it will be no harm to the code.
  5. An interface can’t be marked final.The reason behind this is that the interface is by default abstract and the keywords ‘abstract’ and ‘final’ never go hand-in-hand.
  6. A final variable,as the name specifies, can’t be reassigned a value. A final method can’t be overridden. A final class can’t be extended.
  7. If a reference variable is marked final, the code that alters the change in the state of object the reference variable refers to , will compile fine. The only thing to note is that the reference variable itself can’t be reassigned with a new value.
  8. Non-static members can’t be accessed from the static context. Static methods and variables can’t access the instance members of the class. Watch out for code like :
  9. We can access static members using a null reference.
  10. A method can’t be defined both ‘abstract’ and ‘static’.

Packages & Imports :

  1. The package statement must (at max ) appear only once in a Java source file .If defined, it must be the first statement in the source file. It can’t appear within or after a class declaration.
  2. All classes and interfaces are defined in a package. If we don’t explicitly define a package statement in a class or an interface, it’s a part of Java default package.
  3. Members of a named package can’t access classes and interfaces defined in a default package.
  4. The package and subpackage names are separated using a dot(.) eg : package com.world.education;
  5. Code to import two classes with the same name but different packages will not compile. In order to achieve it, you need to use fully-qualified name for at-least one of these classes.
  6. Import statements can’t precede a package statement.
  7. The import statements doesn’t embed the contents of the imported class in your class, which means that importing more classes doesn’t increase the size of your own class.
  8. You can’t import the classes from subpackages by using an * in the import statement.
  9. All the members of java.lang package are implicitly imported in all the Java source files and therefore need not be explicitly imported.
  10. Static imports allow importing static members of the class by using ‘import static’ statement.(Note :not ‘static import’). By using the ‘import static’ statement to import static members, you don’t need to prefix them with class name.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *