Java Hungry

What are Inner Classes and its types (Regular, Method local,Anonymous ) In Java

Inner classes are one of the most fundamental topics of java . So in this post first we will discuss what are inner classes and what are the different type of inner classes.  Inner classes is an important topic if you are about to give OCJP exam. Although in real world coding , inner classes are not used frequently. But It is better to have an idea , what classes are inner and how to write it in java.


Read Also :  Difference between == and equals method


What is Inner Class ?

Inner class is the class with in a class . It is treated like a member of the outer class and  has access to all the members of the outer class even those which are declared as private .

For example :


class Outer
{
    private name ;
    class Inner {   }         // Inner class can access the name member
                             // of the Outer class  even though it is
                            // declared as private
}


How to compile the above code  :

Simple , just write the name of the outer class and following with .java extension

So , we can compile the above code as

javac   Outer.java

On executing the above line , you will notice that , it produces the twins (that is two classes , in one command )   , namely ,

    Outer.class (expected)
    Outer$Inner.class  (byproduct)

Now , come the turn to execute the byte code

You can not write

java Outer$Inner.java  , As the regular inner class can not have static  declarations of any kind .  The only way you can access the inner class is through the live instance of the Outer Class .
or in other words
One can interact with Inner class at runtime only , after creating the Outer class instance .


RULE OF THUMB :




Core Java Coding / Programming Questions and Answers : Technical Interview in Java

If you want to be a java developer , then you must have faced  core java technical round during the recruitment process . But sadly , most of the students find it the hardest round  in the interview process . The problem lies in the practicing part but it can be overcome . The solution is to prepare must know questions of the technical round . So knowing those questions gives us an edge over others . So here I am sharing  coding/programming questions and answers of core java which are frequently asked by the interviewer in Technical round of  Java programming language.

Read Also  :  Top 25 frequently asked core java interview questions

Due to slow internet connection , some of our readers were not able to view all the questions . So ,
 we decided  to split this image rich post  into two pages. 

Let us discuss one by one ,

*   Interviewer  shows you the following code

 Is the  code compiles ? If yes then what will be the output ?


core java technical questions and answers














 












Answer >>






Reflection : Javap Command Working With Example Java Program Code

Javap Command gives overview of the class file . It is also called java class disassembler.
It gives the information depending upon the options used by the user .

1. Programmer NOT providing any helper option with javap command

Results in javap command shows all the package,public and protected fields and methods of the classes passed . We can also make the similar program with the help of reflection . Reflection is one of the core features of java allowing access to the Class class object .

javap command example without using options  :

Compile the following class JavapExample


public class JavapExample {

  String firstname ;
  String lastname ;
  
  public  void printName(){
   System.out.println("Full name of the candidate is " + firstname + lastname);
  }
 
  public void printNameUSAformat(){
   System.out.println("Full name of the candidate in reverse is " + lastname + firstname);
  }
}
  
The output from javap  JavapExample.class  will be :

Compiled from "JavapExample.java"

public class JavapExample extends java.lang.Object {
  java.lang.String firstname;
  java.lang.String lastname;
  public JavapExample();
  public void printName();
  public void printNameUSAformat();

}


2. Other options Programmer has for the javap command are as follows :