/**
 * 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();
package com.javadb2.mugil.employee;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.javadb2.mugil.db.display;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class update
{	 
	public static void main(String args[])
	{
	  bean objBean = new bean();
	  objBean.setEmpId(Integer.parseInt(getInput("Enter Employee Id :")));
	  objBean.setEmpName((String)getInput("Enter Employee Name:"));
	  objBean.setEmpSalary(Integer.parseInt(getInput("Salary")));
	  updateValues(objBean);
	}
	
	public static void updateValues(bean objBean)
	{	
		System.out.println(objBean.getEmpName());
		System.out.println(objBean.getEmpSalary());
		
		String strSQL = "UPDATE employeelist" +
				"   SET empname = ? ," +
				"       salary  = ?" +
				" WHERE empid = ?";
		
		try 
		{
			Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "pass");
			PreparedStatement stmt = (PreparedStatement) conn.prepareStatement(strSQL);
			
			stmt.setString(1, objBean.getEmpName());
			stmt.setDouble(2, objBean.getEmpSalary());
			stmt.setInt(3, objBean.getEmpId());
			
			int affected = stmt.executeUpdate();
						
			if(affected == 1)
			{
 			  System.out.println("Updated Successfully");
			}
			else
			{
			  System.err.println("Unable to Update Employee");
			}
		} 
		catch (SQLException e) 
		{
		  e.printStackTrace();
		}
	}

	public static String getInput(String prompt)
	{
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.print(prompt);
		System.out.flush();
		
		try 
		{
			return stdin.readLine();
		} 
		catch (Exception e) 
		{
			// TODO: handle exception
			return e.getMessage();
		}
	}
}	

Database Table

 CREATE TABLE employeeList(empid int not null primary key auto_increment,
                           empname varchar(255),
                           salary  double);

Bean Class

package com.javadb2.mugil.employee;

public class bean
{
  private int  empId;
  private String  empName;
  private double  empSalary;
	
  public int getEmpId() 
  {
    return empId;
  }
	
  public void setEmpId(int empId) 
  {
    this.empId = empId;
  }
	
  public String getEmpName() 
  {
    return empName;
  }
  
  public void setEmpName(String empName) 
  {
    this.empName = empName;
  }
  
  public double getEmpSalary() 
  {
    return empSalary;
  }

  public void setEmpSalary(double empSalary) 
  {
    this.empSalary = empSalary;
  }
}

Crud Operation with Bean Class

 package com.javadb2.mugil.employee;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.javadb2.mugil.db.display;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class insert
{	
  public static void main(String[] args) 
  {
    bean objBean = new bean();
		
    objBean.setEmpName(display.getInput("Enter the Employee Name"));
    objBean.setEmpSalary(Integer.parseInt(display.getInput("Enter Salary")));
		
    boolean result = getInsertedKey(objBean);
		
    if(result)
    {
      System.out.println("The Last Inserted Primary Key is "+objBean.getEmpId());			 
    }
   }
	
   public static boolean getInsertedKey(bean objBean)
   {	
     ResultSet keys = null;
		
     String strSQL = "INSERT INTO employeeList(empname, salary)VALUES(?,?)";		
		
     try 
     {
	Connection conn 	= getConnection();
	PreparedStatement stmt 	= (PreparedStatement) conn.prepareStatement(strSQL, Statement.RETURN_GENERATED_KEYS);
			
	stmt.setString(1, objBean.getEmpName());
	stmt.setDouble(2, objBean.getEmpSalary());
			
	int affected = stmt.executeUpdate();
			
	if(affected == 1)
	{
  	   keys = stmt.getGeneratedKeys();
	   keys.next();
	   int newkey = keys.getInt(1);
	   objBean.setEmpId(newkey);
	}
	else
	{
	   System.err.println("Unable to Update Values");
	   return false;
	}
      }
      catch (SQLException e) 
      {
	 processException(e);
	 System.err.println("Error in Establishing Connection");
      }
		
      return true;
  }
	
  public static Connection getConnection() throws SQLException
  {
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "pass");
    return conn;
  }
	
  public static void processException(SQLException e)
  {
     System.err.println("Error Message "+e.getMessage());
     System.err.println("Error Code"+e.getErrorCode());
     System.err.println("SQL State"+e.getSQLState());
  }
}