Local Inner Classe in Java

Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. … These classes have access to the fields of the class enclosing it. Local inner classes are must be instantiated in the block they are defined in.

Basic Object Construction and Use in Java

Objects come Basic Object Construction and Use in Java in their own class, so a simple example would be a car (detailed explanations below):

public class Car {
     //Variables describing the characteristics of an individual      car, varies per object
     private int milesPerGallon;
     private String name;
     private String color;
     public int numGallonsInTank;

     public Car(){
          milesPerGallon = 0;
          name = "";
          color = "";
          numGallonsInTank = 0;
     }
     //this is where an individual object is created
     public Car(int mpg, int, gallonsInTank, String carName, String carColor){
          milesPerGallon = mpg;
          name = carName;
          color = carColor;
          numGallonsInTank = gallonsInTank;
     }

      //methods to make the object more usable

     //Cars need to drive
     public void drive(int distanceInMiles){
        //get miles left in car
        int miles = numGallonsInTank * milesPerGallon;
        //check that car has enough gas to drive distanceInMiles
       if (miles <= distanceInMiles){
           numGallonsInTank = numGallonsInTank - (distanceInMiles / milesPerGallon)
           System.out.println("Drove " + numGallonsInTank + " miles!");
       } else {
        System.out.println("Could not drive!");
       }
    }

     public void paintCar(String newColor){
          color = newColor;
     }
         //set new Miles Per Gallon
     public void setMPG(int newMPG){
         milesPerGallon = newMPG;
     }
         //set new number of Gallon In Tank
     public void setGallonsInTank(int numGallons){
         numGallonsInTank = numGallons;
     }
     public void nameCar(String newName){
         name = newName;
     }
     //Get the Car color
     public String getColor(){
          return color;
     }
     //Get the Car name
     public String getName(){
           return name;
     }
     //Get the number of Gallons
      public String getGallons(){
           return numGallonsInTank;
     }
}

Objects are instances of their class. So, the way you would create an object would be by calling the Car class in one of two ways in your main class (main method in Java or onCreate in Android).

Option 1
`Car newCar = new Car(30, 10, "Ferrari", "Red");

Option 1 is where you essentially tell the program everything about the Car upon creation of the object. Changing any property of the car would require calling one of the methods such as the repaintCar method.

Example: newCar.repaintCar("Blue");

Note: Make sure you pass the correct data type to the method. In the example above, you may also pass a variable to the repaintCar method as long as the data type is correct`.

That was an example of changing properties of an object, receiving properties of an object would require using a method from the Car class that has a return value (meaning a method that is not void). Example:

String myCarName = newCar.getName(); //returns string "Ferrari"

Option-1 is the best option when you have all the object’s data at the time of creation.

Option 2

`Car newCar = new Car();

Option-2 gets the same effect but required more work to create an object correctly. I want to recall this Constructor in the Car class:

public void Car(){
     milesPerGallon = 0;
     name = "";
     color = "";
     numGallonsInTank = 0;
}

Notice that you do not have to actually pass any parameters into the object to create it. This is very useful for when you do not have all the aspects of the object but you need to use the parts that you do have. This sets generic data into each of the instance variables of the object so that, if you call for a piece of data that does not exist, no errors are thrown.

Related Article: Java Nested and Inner Classes

Note: Do not forget that you have to set the parts of the object later that you did not initialize it with. For example,

Car myCar = new Car();
String color = Car.getColor(); //returns empty string

This is a common mistake amongst objects that are not initialized with all their data. Errors were avoided because there is a Constructor that allows an empty Car object to be created with stand-in variables (public Car(){}), but no part of the myCar was actually customized. Correct example of creating Car Object:

Car myCar = new Car();
myCar.nameCar("Ferrari");
myCar.paintCar("Purple");
myCar.setGallonsInTank(10);
myCar.setMPG(30);

And, as a reminder, get an object’s properties by calling a method in your main class. Example:

String myCarName = myCar.getName();     //returns string "Ferrari"

Simplest Possible Class

class TrivialClass {}

A class consists at a minimum of the class keyword, a name, and a body, which might be empty.
You instantiate a class with the new operator.

TrivialClass tc = new TrivialClass();

Object Member vs Static Member

With this class:

class ObjectMemberVsStaticMember {

      static int staticCounter = 0;
      int memberCounter = 0;

      void increment() {
           staticCounter ++;
           memberCounter++;
      }
}

the following code snippet:

final ObjectMemberVsStaticMember o1 = new ObjectMemberVsStaticMember();
final ObjectMemberVsStaticMember o2 = new ObjectMemberVsStaticMember();

o1.increment();

o2.increment();
o2.increment();

System.out.println("o1 static counter " + o1.staticCounter);
System.out.println("o1 member counter " + o1.memberCounter);
System.out.println();

System.out.println("o2 static counter " + o2.staticCounter);
System.out.println("o2 member counter " + o2.memberCounter);
System.out.println();

System.out.println("ObjectMemberVsStaticMember.staticCounter = " +
ObjectMemberVsStaticMember.staticCounter);

// the following line does not compile. You need an object
// to access its members
//System.out.println("ObjectMemberVsStaticMember.staticCounter = " +
ObjectMemberVsStaticMember.memberCounter);

produces this output:

o1 static counter 3
o1 member counter 1

o2 static counter 3
o2 member counter 2

ObjectMemberVsStaticMember.staticCounter = 3

Note: You should not call static members on objects, but on classes. While it does not make a difference for the JVM, human readers will appreciate it.

static members are part of the class and exists only once per class. Non-static members exist on instances, there is an independent copy for each instance. This also means that you need access to an object of that class to access its members.

What is the Local Inner Class?

A class i.e. created inside a method is called local inner class in java. If you want to invoke the methods of local inner class, you must instantiate this class inside the method.

public class localInner1{
    private int data=30;//instance variable
    void display(){
      class Local{
         void msg(){System.out.println(data);}
         }
        Local l=new Local();
        l.msg();
        }
        public static void main(String args[]){
             localInner1 obj=new localInner1();
             obj.display();
        }
}

Leave a Comment