Google+

188. Creating more than one Class in a single Java Class file







Till now we have created separate Java Class files for each and every Class in our Java Projects. But we can have more than one Class in a single Java Class file with the below few restrictions -
  1. Only one Class need to be Public 
  2. Public Class name and Java Class file name should be same
  3. main( ) method shouldn't be there in the non-public classes.
Hoping that you have understood the above three restrictions, lets create a  single .java class file and write more than one classes into it.

Lets implement this on Eclipse IDE -

1.Launch Eclipse IDE, Create a Java Project 'Project 22' as shown below -




2. Create a  Java Class file 'ClassOne'  as shown below -



3. Create another Public Class 'ClassTwo' in the same file i.e. 'ClassOne.java' as shown below -



4. View the error displayed in the above step as shown below -



This error is displayed as the Java don't accept more than one public Class in a single .java Class file, i.e. Only one Class can be public when more than one Classes are there in a single Java Class file.

5.  Remove the public access specifier from the Class 'ClassTwo' and observe that the error got resolved as shown below -



6. Create main( ) method in Class 'ClassTwo'  as shown below -



7. Run the .java Class file 'ClassOne.java' as shown below and observe that 'Run As' -> 'Java Application' option is not available as shown below -



'Run As' -> 'Java Application' . is not available as the Class name having main( ) method i.e. ClassTwo is not same as the .java Class file name 'ClassOne.java' that we are trying to Run. Hence 'Run As' -> 'Java Application' is not available. To resolve this problem move the main( ) method from 'ClassTwo' to 'ClassOne' by following the below steps.

8. Move the main( ) method from 'ClassTwo' to 'ClassOne' as shown below -



9. Now Right Click on  the .java Class file 'ClassOne.java' and observe that 'Run As' -> 'Java Application' option is available -


Java sets a rule that the .java Class file name 'ClassOne.java' should be same as the Class name which is having the main( ) method i.e. 'ClassOne' . Hence 'Run As' -> 'Java Application' is available as shown below. If not you wont get any compile error, but you wont be able to use the 'Run As' -> 'Java Application'

10. In 'ClassTwo' , create a method 'two' with print statement as shown below -


11. As we cannot access the instance variables and methods in the 'ClassTwo' from 'ClassOne' , we've to create object for 'ClassTwo' in 'ClassOne' as shown below -


12. Now access and call the method two( ) in ClassTwo using the created object as shown below -


13. Run the .java Class file 'ClassOne.java' and observe that print statements in both the Classes got printed in the output as shown below -








Please comment below to feedback or ask questions.

Objects cant be created for abstract Classes  will be explained in the next post.






187. varargs parameter must be used only once in a method







Pre-requisite -


While using varargs concept, the Java Compiler will throw compile time errors if we use it wrongly. According to Java, when you use varargs parameter more than once in any method, Java will throw compile time error. So use varargs parameter only once in any method.

Example for varargs which throws Compile time error -

void sum(int ... var1, int ... var2)
{
        //Method's code
}

As this example is using more than one varargs parameter in the method, Java will throw compile time errors when compiled.

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, open the Java Project  'Project 21' created in previous post,  create a Java Class 'VarargsError2Demo' as shown below -


 2. Crete a method sum( ) containing more than one varargs parameter as shown below  -



3. View the error and observe that an error is displayed as shown below -


Though the last two parameters in the above method sum( ) are varargs parameters, compiler is throwing error for last but one varargs parameter. Hence we cant have more than one varargs parameters in any method.

4. Now resolve that error by removing the last varargs parameter and observe that the error got resolved as shown below -



Hence we cant use more than one varargs parameters in a method.





Please comment below to feedback or ask questions.

Creating more than one Class in a single Java Class file will be explained in the next post.













186. varargs parameter must be the last parameter in the method







Pre-requisite -


While using varargs concept, the Java Compiler will throw compile time errors if we use it wrongly. According to Java, when you use varargs parameter in any method, you must make sure that varargs parameter must be the last parameter.

Example for varargs which throws Compile time error -

void sum(int ... var1, int var2)
{
        //Method's code
}

As this example is using varargs parameter in the first place of all the parameters of the method, Java will throw compile time errors when compiled.

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Project  'Project 21' as shown below -


2. Create a Class 'VarargsError1Demo' as shown below -


3. Create a static method sum( ) by providing varargs parameter as first parameter in the method as shown below -


4. Select 'Change project compliance and JRE to 1.5' option in the displayed error message as shown below -


5. Observe that a compile time error with message 'The variable argument type int of the method sum must be the last parameter' is displayed as shown below -


6. Now move the varargs parameter to the last as shown below and observe that the error got resolved -



Hence, varargs parameter must be the last parameter in any method.






Please comment below to feedback or ask questions.

varargs parameter must be used only once in a method  will be explained in the next post.




 

185. varargs 'Variable length arguments'







varargs (variable length arguments) is a unique concept in Java.

Reason behind the origin of varargs -
  1. What will happen if you don't know how many arguments you need to be passed to a method ?
  2. What will happen if you want to pass unlimited variables to a method ?
In order to answer the above two questions, Java has introduced a unique concept called varargs.

Using varargs you can pass as many as arguments of the same type to any method. So varargs allows the method to accept zero to unlimited number of arguments.

Syntax of varargs -

varargs (variable length argument) is specified by three periods (...)
In Syntax, we need to specify these three periods (...) after the data type as shown below -

return_type method_name(data_type ... variable_name)
{   
     //Method code
}

Example of varargs -

void sum(int... var)
{
     //Method code 
}

Lets implement varargs on Eclipse IDE -

1. Launch Eclipse IDE, create a Java Project 'Project 019' as shown below -



2. Create a Class 'VarargsDemo' as shown below -



3. Create a method sum( ) by specifying integer type varargs argument as shown below -



4. Observe that an error is displayed on the above step, click on the error and view the error message as shown below -


This error is displayed even though I have installed Java version 1.8 in my system which is greater than 1.5.

Why is this error displayed and how to resolve this error ?

Though we have installed Java version 1.8 in our system, the Eclipse IDE has set the Compiler compliance level which is less than 1.5.

How to resolve this error ?

We can resolve this error by selecting the 'Change project compliance and JRE to 1.5' option in the above displayed error dialog or just follow the below steps to change the compiler compliance level from a differenr place in Eclipse IDE.

5. Right click on the Java Project 'Project 019' and select the 'Properties' option

6. In 'Properties for Project 019' dialog, select 'Java Compiler' on the left and observe that 'Compiler Compliance Level' option is displayed on the right as shown below -



7. Observe that the Compiler Compliance Level is set to 1.4 (i.e. less than 1.5) by default in my machine as shown below -


8. Change the compliance level to 1.5 and click on 'Apply' followed by 'OK' buttons as shown below -


9. Click on 'Yes' button to rebuild the project if any dialog asking to rebuild is displayed

10. Observe that the error got resolved in the VarargsDemo Class as shown below -



11. Now write the code to calculate the sum of all the integer arguments passed to the varargs argument as shown below -


12. Print the calculated sum as shown below -



13. Now create main( ) method in the same Class to call the sum( ) method by passing different number of parameters to the same method as shown below -


14. Observe that errors are displayed in the above screen. This is because, as the main( ) is a static method, it can only access the methods that are static only. Hence it is throwing these errors. In order to resolve make the sum( ) method as static as shown below and observe that the errors got resolved -


15.  Run the Class and observe that the sum( ) method has accepted different number of arguments passed to it using varargs arguments and the sum of the passed arguments is calculated and printed in as output in the console as shown below -









Please comment below to feedback or ask questions.

varargs parameter must be the last parameter in the method will be explained in the next post.




184. For-each loop







For-each loop is a form of for loop which can be used with arrays. For-each loop iterates for each element of the array.

Example -

int var[ ] = { 1, 2, 3, 4, 5 };

The above array has 5 elements, hence the for-each loop iterates for 5 times (i.e. each time for every element in the array )

Syntax of For-each loop for arrays -

for(data_type variable : array)
{

}

Example of For-each loop for arrays -

int var[ ] = { 1, 2, 3, 4, 5 };

for(int x : var )
{
      System.out.println(var[x-1]);
}

Observe that in the above for-each loop, the variable x will be assigned 1 (i.e. 1st element of the array).

So, when you are print the array element, as the index of the array starts with 0, mention var[x-1] (i.e.. var[0] ) instead of var[x] (i..e var[1] ).

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a Java Project 'Project 20' as shown below -


2. Create a Java Class 'ForEachLoopDemo' with main( )  method as shown below -


3. Declare an type of array and assign any 5 values to it as shown below -


4. Create a for-each loop to print all the values of the array as shown below -


5. Observe that an error is displayed and view the error as shown below -


6. Though I have installed Java version 1.8 in my machine, the compiler compliance level in Eclipse for the Java Projects is set to a version level that is less than 1.5 and for-each loop feature is introduced in Java from version 1.5. Hence this error is displayed. To resolve this error select 'Change Project compliance and JRE to 1.5' option displayed in the error message as shown below  -


7. Observe that the error got resolved as shown below -


8. Run the Class and observe that all the 5 values stored the array are displayed as output in the console as shown below -



Notes -

  1. For-each loop can also be used with varargs ( varargs concept will be explain in the next post)
  2. For-each loop can also be used with collections (collections concept will be explained later in this blog) 





Please comment below to feedback or ask questions.

varargs 'Variable Length Arguments' will be explained in the next post.