/**
 * Referencing Method-Local Inner Class
 */
public class Outer6 
{	
	private static int x = 25;
	
	public static void main(String[] args) 
	{	
		invokeInnerClass();
	}
	
	public static void invokeInnerClass()
	{	
	   class Inner6
	   {	
		 public void displayInnerMsg()
		 {
		   System.out.println(x);
		   System.out.println("I am a Inner Class");
		 }
	   }
		
		Inner6 objInner6 = new Inner6();
		objInner6.displayInnerMsg(); 
	}
}

Output

25
I am a Inner Class

The Inner Class Defined with in static method has access to only static variables defined in the outer class

 /**
 * Referencing Method-Local Inner Class
 */
public class Outer6 
{	
	public static void main(String[] args) 
	{
		Outer6 objOuter6 = new Outer6();
		objOuter6.invokeInnerClass();
	}
	
	public void invokeInnerClass()
	{
		class Inner6
		{
			public void displayInnerMsg()
			{
				System.out.println("I am a Inner Class");
			}
		}
		
		Inner6 objInner6 = new Inner6();
		objInner6.displayInnerMsg(); 
	}
}

Output

 I am a Inner Class

You cannot Access local variable of the Method since variables are stored in Stack and object exists in Heap memory.The Stack memory will be blown away once the Method exits.

You can access a variable marked as Private in Outer Class.

You can access local variable marked as Final.

The class inside Method-Local Inner class can be either abstract or final.Other private, public and protected are not allowed

/**
 * Referencing Outer Class Object and Its Value
 */
public class Outer5 
{	
	private int x = 7;
	
	public static void main(String[] args) 
	{
		Outer5 objOuter5 = new Outer5();
		objOuter5.callInnerClassMethod();
	}
	
	public void callInnerClassMethod()
	{
		Inner5 objInner5 = new Inner5();
		objInner5.InnerClassMethod();
	}
	
	public class Inner5
	{
		private int x = 5;
	
		public void InnerClassMethod()
		{
		 System.out.println(x);
		 System.out.println(Outer5.this.x);
		}
	}
}

Output

5
7
public class Outer1
{
	public static void main(String[] args) 
	{
		Outer2 objOuter1 = new Outer2();
		Outer2.Inner1 objInner1 = objOuter1.new Inner1();  
		objInner1.innerMethod1();
	}
}

class Outer2 
{	
	public void makeInner()
	{
		Inner1 objInner1 = new Inner1();
		objInner1.innerMethod1();
	}
	
	class Inner1
	{
		public void innerMethod1()
		{
			System.out.println("This is Inner Method1");
		}
	}
}

Output

This is Inner Method1
public class Outer1
{
	public static void main(String[] args) 
	{
		Outer2 objOuter1 = new Outer2();
		Outer2.Inner1 objInner1 = objOuter1.new Inner1();  
		objInner1.innerMethod1();
	}
}

class Outer2 
{	
	public void makeInner()
	{
		Inner1 objInner1 = new Inner1();
		objInner1.innerMethod1();
	}
	
	class Inner1
	{
		public void innerMethod1()
		{
			System.out.println("This is Inner Method1");
		}
	}
}

The equals method that comes along java.lang.Object provides an equal method which does the following

<pre>
public boolean equals(Object o)
{
if(this == 0) return true;
}
</pre>

But the above implementation only for Integer, Boolean, Character and Other Wrapper classes because it is overridden.

When we define our own class and define equals the below is how its going to be.

<pre>

class mac
{
int Age;
}
</pre>

Now the below code does not give any output until equals method is overridden if you comment out   objmac2 = objmac1;      .

<pre>
Integer num1 = new Integer(5);
Integer num2 = new Integer(5);

mac objmac1 = new mac();
mac objmac2 = new mac();

objmac2 = objmac1;

objmac1.Age = 25;
objmac2.Age = 25;

if(objmac1 == objmac2)
System.out.println(“They are same”);

if(objmac1.equals(objmac2))
System.out.println(“Objects are same”);

</pre>

 

 

 

 

 

 

package packA;

public class sam1 
{
 public static final double DIAMETER = 12756.32; // kilometers
 
 public static void hithere()
 {
	 System.out.println("Hi There");
 } 
}


package packB;
import packA.sam1;

public class sam2 
{
	public static void main(String args[])
	{
		sam2 objsam2 = new sam2();
		objsam2.halfway();		
	}
	
	public void halfway()
	{ 
		sam1.hithere();
		System.out.println(sam1.DIAMETER/2.0);
	}
}

-Two Packages – packA and packB – While importing static from packA to packB you should either use import packA.sam1; or import static packA.sam1.*;

Using import static packA.sam1; will not allow to access elements in package

If you use import packA.sam1;
className.StaticVariableName

If you use import static packA.sam1.*;
StaticVariableName

The below code generates no compilation error in eclipse but throws error during Runtime.

public class Animal
{
  public void eat(){}
}
public class Dog extends Animal
{
  public void eat(){}
  public void main(String[] args)
  {
    Animal animal=new Animal();
    Dog dog=(Dog) animal;
  }
}

Output

Exception in thread "main" java.lang.ClassCastException: com.mugil.wild.Animal cannot be cast to com.mugil.wild.Dog
	at com.mugil.wild.Dog.main(Dog.java:12)

By using a cast you’re essentially telling the compiler “trust me. I’m a professional, I know what I’m doing and I know that although you can’t guarantee it, I’m telling you that this animal variable is definitely going to be a dog

Because you’re essentially just stopping the compiler from complaining, every time you cast it’s important to check that you won’t cause a ClassCastException by using instanceof in an if statement.

Generally, downcasting is not a good idea. You should avoid it. If you use it, you better include a check:

Animal animal = new Dog();

if (animal instanceof Dog)
{
Dog dog = (Dog) animal;
}

public class ClassA 
{	
	public void MethodA()
	{
		System.out.println("This is Method A");
	}
}

public class ClassB extends ClassA
{	
	public void MethodB()
	{
		System.out.println("I am Method in Class B");
	}
}


public class ClassC 
{
	public static void main(String[] args) 
	{
		ClassA objClassA1 = new ClassA();
		ClassB objClassB2 = new ClassB();
		
		//Child Class of Parent Type can be Created  
		ClassA objClassB1 = new ClassB();
		
		//Assigning a Parent class Type to Child Class is Not Allowed  
		//Casting Should be Carried out
		ClassB objClassA2 = (ClassB) new ClassA();
		
		objClassA1.MethodA();
		objClassB2.MethodA();
		objClassB2.MethodB();
		
		objClassB1.MethodA();
	}
}

In java String Builder Should be Used in case you need to perform concatenate more string together.

i.e

 public String toString()
 {
    return  a + b + c ;
 }

For the above code using + will be converted to

a = new StringBuilder()
    .append(a).append(b).append(c)
    .toString();

For the above case you can use concat as below but since + will be converted as String Builder its better to use + rather than concat.

 public String toString()
 {
    return  a.concat(b).concat(c);
 }

The key is whether you are writing a single concatenation all in one place or accumulating it over time.

There's no point in explicitly using StringBuilder.

But if you are building a string e.g. inside a loop, use StringBuilder.

To clarify, assuming that hugeArray contains thousands of strings, code like this:

...
String result = "";
for (String s : hugeArray) {
    result = result + s;
}

It should be as below

 ...
StringBuilder sb = new StringBuilder();

for (String s : hugeArray) {
    sb.append(s);
}
String result = sb.toString();