Skip to main content

Declaration and Access Modifier

                                                 Declaration and Access Modifier

  • java source file structure
A java program can contain any number of classes but at most one class can be declared as public.if there is a public class the name of the program and the name of the public class must match other wise we will get compile time error.

/*
case 1-:if there is no public class then we can use any name and there are no restrictions.

 class A{
}
class B{
}
class C{
}
class D{
}
save program name any classname.java

case 2-: if class B is public then name of program should be B.java other wise we will get compile time error saying class B is public , should be declared in a file named B.java.

class A{
}
public class B{
}
class C{
}
class D{
}


case 3-:if class B and C declared as public and name of program is B.java then we will get compile time error saying class C is public should be declared in a file named C.java

class A{
}
public class B{

}
public class C{
}
class D{
}


Note-:
  • whenever compiling a java program for every class present in the program a separate (.class) file will be generated.
  • we can compile java program(java source file) but we can run a java dot class (.class)
  • whenever exciting a java class the corresponding class main method will be executed.
  • if the class doesn't contain the main method then we will get a run time exception saying noSuchMethodError: main
  • if the crossponding(.class file) is not available then we will get a run time exception saying noClassDefFoundError
  • it is not recommended to declare multiple classes in a single source file, it is highly recommended to declare only one class per source file and the name of the program we have to keep the same as the class name. the main advantage of this approach is readability and maintainability of the code will be improved.
Example-:

class A{
    public static void main(String... args){
        System.out.print("hello");
    }
}
class B{
    public static void main(String... args){
        System.out.print("hello");
    }
}
 class C{
      public static void main(String... args){
        System.out.print("hello");
    }
}
class D{
}


java A 
output-: hello

java B
output-: Hi

java C
output -: Bey

java D
Run time Error-:NoSuchMethodError : main
java Test
Run time Error-:NoClassDefFoundError :Test

 

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...