Q:
Java program to create members with access modifier
belongs to collection: Java Class and Object Programs
Java Class and Object Programs
- Java program to print message using class
- Java program to find area and perimeter of a circle using class
- Java program to count digits of a number using class
- java program to find sum and product of all digits of a number using class
- Java program to check whether entered number is palindrome or not using a class
- Java program to check Armstrong number
- Java program to read and add two distances using class
- Java program to create members with private access modifier
- Java program to create members with access modifier
- Java program to create a singleton class
- Java program to create instances of singleton class and print hash codes
- Java program to compare instances of singleton class
- Java program to create a singleton class with method name as that of class
- Java program to implement cascaded method call
- Java program to create a method returning an object
- Java program to pass an object as an argument
- Java program to create an array of objects
- Java program to implement default or no-argument constructor
- Java program to implement constructor overloading
- Java program to implement a copy constructor
- Java program to implement constructor chaining
- Java program to create an object of a class as a data member in another class
- Java program to create an object of a class as a data member in another class
- Java program to demonstrate the instanceof operator
- Java program to create an anonymous object
- Java program to call a method using the anonymous object
- Java program to implement cascaded method call with an anonymous object
- Java program to check anonymous class using isAnonymousClass() method
- Java program to check whether the specified Class object represents an array or not
- Java program to check whether the specified Class object represents a primitive type or not
- Java program to check whether the specified Class object represents an interface type or not
- Java program to check whether an object belongs to a particular class or not
- Java program to demonstrate the newInstance() method
- Java program to get the class name using the object of the class
- Java program to represent the class in different ways
- Java program to call static block using the anonymous object
- Java program to check whether a class is a local class or not
- Java program to check whether a class is a member class or not
- Java program to check whether a class is declared as an enum or not
- Java program to check an annotation type using the isAnnotation() method
- Java program to demonstrate the getName() method
- Java program to get the simple name of the class
- Java program to print the class loader of the given class
- Java program to get the name of the superclass
- Java program to get the name of the generic superclass
- Java program to get the list of implemented interfaces in a class
- Java program to get the package name of a class
- Java program to get the list of fields of a class
- Java program to get the list of defined classes and interfaces in a class
- Java program to get the list of methods of a class
In this program, we will create two members without specifying any modifier and access them inside or outside the class.
Note: If we do not specify any access modifier then by default it is known as default modifier. These types of members can be accessed outside the class but can not be accessed outside the package.
Program/Source Code:
The source code to create members with access modifier is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created two classes Demo and Main. The Demo class contains two members num1, num2 without any access modifier. And, printed the value of members using the printValues() method and also modify the values of members.
The Main class contains a static method main(). The main() is an entry point for the program. Here, we created the object of Demo class and called printValues() method to values of num1 and num2. After that, we printed the values of num1 and num2 directly.