package com.apryll.package2;

public class sample2 
{
  public static void main(String args[])  
  {
    sample2 objsample2 = new sample2();
    objsample2.argCount(1, 2, 3, 4);
  }
	
  public void argCount(int num1, int num2, int... args)
  {
    System.out.println(num1);
    System.out.println(num2);
    System.out.println(args.length);
  }
}

OP
1
2
2

Please Note the Last 2 which accounts for the array argument args as num1 and num2 does not account for array arguments

If the Code is like below then it would have been a error

  public void argCount(int num1, int num2, int... args)

How to Loop Through Arguments Passed to function

package com.apryll.package2;

public class sample2 
{
 public static void main(String args[])  
 {
  sample2 objsample2 = new sample2();
  objsample2.argCount(1, 2, 3, 4);
 }
	
 public void argCount(int... args)
 {
   System.out.println(args.length);
 }
}

Sample1.java

package com.apryll.package1;
//import static com.apryll.package2.sample2.Funct3;

public class Sample1 
{
	public static void main(String[] args) 
	{
		Sample1 objSample1 = new Sample1();		
		//sample2 objSample2 = new sample2();
		
		System.out.println(com.apryll.package2.sample2.City);
		
		objSample1.Funct2();
		//objSample2.Funct4();
		
		//Sample1.Funct1();
	}
	
	static void Funct1()
	{
	  System.out.println("I am Static Function 1");
	}
	
	void Funct2()
	{	
	  System.out.println(Funct3());
	  System.out.println(City);
	}		
}

Sample2.java

 package com.apryll.package2;

  public class sample2 
  {
     public static int City = 555;
     public static String Funct3()  
     {
	return "Hi there";
     }
	
     public void Funct4()  
     {
	System.out.println("Hello There");
     }
}

1

  package com.apryll.package1;
  
  import  com.apryll.package2.sample2.*;

  public class Sample1 
  {
    public static void main(String[] args) 
    {
      sample2 objSample2 = new sample2();	
    }
  }  

The Above Code Does not Work when creating a object for sample2 since in the import statements is sample2.* is given.It should be

import  com.apryll.package2.sample2;
.
.
.
.

2
While Importing class methods and variables the static will not get imported unless you specify static
import in the import statement

You can Import the Static Methods and Variables in class as below

To Import static methods and Variables

  import  static com.apryll.package2.sample2.*;  

To Import static methods and Variables

  import  static com.apryll.package2.sample2.*;  

To Import static Variable City

  import  static com.apryll.package2.sample2.City;  

To Import static Function getCityName

  import  static com.apryll.package2.sample2.getCityName;  

3
You can directly access static method and Variable declared in some other package in static void main() method as follows

  System.out.println(com.apryll.package2.sample2.City);
  System.out.println(com.apryll.package2.sample2.Funct3());

How to call Static Method in Another Package
Sample4.java

package com.scjp.test;

public class Sample4 
{
  public static void CallMe()
  {
   System.out.println("I am Static in Another Package");	
  }

  public static void main(String[] args) 
  {	
  }
}

Sample6.java

import com.scjp.test.*;

public class Sample6 
{
  public static void main(String[] args) 
  {
    Sample4.CallMe();
  }
}

Method name same as Class Name
void Sample3 is a method not a Constructor

package com.scjp.test;

public class Sample3 
{	
	void Sample3()
	{
		System.out.println("I am Constructor");
	}
	public static void main(String[] args) 
	{
		Sample3 objSample3 = new Sample3();
	}
}

OP:

Overloading Constructors

package com.scjp.test;

class Sample3
{
  public static void main(String args[])
  {
    Vasko objVasko1 = new Vasko(54);
    Vasko objVasko2 = new Vasko();
    System.out.println(objVasko1.Size);
    System.out.println(objVasko2.Size);
  }	
}

class Vasko
{
   int Size;

   Vasko()
   {	  
   }
	
   Vasko(int s)
   {
    Size = s;	  
   }
}

OP:

54
0

How Static and Constructor will work when class is Extended

  public class Main2 extends A   
  {
   public static void main(String args[])
    {	
	   
    }
  }

  class A extends B 
  {
    A()
    {
      System.out.println("I am A Constrctor");
    }
	
    static
   {
      System.out.println("I am Static Method");
   }
	
   {
      System.out.println("Empty");
   }
 }

 class B
 {
   B()
   {
     System.out.println("I am B Constrctor");
   }
 }

OP:I am Static Method

How Static and Constructor will work when class object is Created

   public class Main2 extends A   
  {
   public static void main(String args[])
    {	
       A objA = new A();  
    }
  }

  class A extends B 
  {
    A()
    {
      System.out.println("I am A Constrctor");
    }
	
    static
   {
      System.out.println("I am Static Method");
   }
	
   {
      System.out.println("Empty");
   }
 }

 class B
 {
   B()
   {
     System.out.println("I am B Constrctor");
   }
 }

OP:I am Static Method
I am B Constrctor
Empty
I am A Constrctor

What would be the Output

public class Main 
{	
  static
  {
    System.out.println("My Name is Mugil");
  }
}

OP: My Name is Mugil
followed by exception in Main

public class Main 
{	
  static
  {
    System.out.println("My Name is Mugil");
  }
	
  public static void main(String args[])
  {
    {
      System.out.println("What is your Name");	 
    }
  }
}

OP: My Name is Mugil
What is your Name

public class Main 
{	
  public static void main(String args[])
  {
    {
      System.out.println("What is your Name");
    }
   }
}

OP: What is your Name

4.What is the order they are processed

public class Main 
{	
 public static void main(String args[])
 {
   {
     ClassA objA = new ClassA();
     System.out.println("What is your Name"); 
   }
 }
}

class ClassA 
{
  static
  {
    System.out.println("I am in ClassA Static");
  }
	
  ClassA()
  {
    System.out.println("I am in ClassA Constrctor");  
  }

  {
    System.out.println("I am in ClassA");
  }
}

OP:I am in ClassA Static
I am in ClassA
I am in ClassA Constrctor
What is your Name

Static blocks are initiated at first followed by Constructor followed by classA and content in empty class.

5.How to Use Object to invoke function in Class

public class Main2
{
 public static void main(String args[])
 {	
   new A().showName();
 }
}

class A 
{
  public void showName()
  {
    System.out.println("Hi there");
  }
}

OP : Hi there

6.How to Find Class of object

  public class Main3 
  {
   public static void main(String[] args) 
   {
     ClasB objB = new ClasB();
     
     if(objB instanceof ClasB)
     {
       System.out.println(objB.getClass().getName());	
     }		
    }
  }

  class ClasB
  {	
  }

Final Modifier Assures that a Reference variable cannot be referenced to a different Object

 public class Main 
 {	
   public int Age;
   public static void main(String args[])
   { 	
     final Main objMain = new Main();
     Main objMain2 = new Main();
     objMain = objMain2;
   }	
 }

OP:Generates a Exception
The final local variable objMain cannot be assigned.