Declarations & Access Control in Java
by Vibrant Publishers
Introduction
In Java, creating a variable is also referred to as declaring a variable. To create a variable in Java, you must specify its type and name. This blog will talk about how declaration in Java works and the steps to create (declare) a variable and its different types such as Local variables, Instance variables. We will also learn constructors, Java access modifiers.
Source File Declaration:
A Java file can have only one public class. If one source file contains a public class, the Java filename should be the public class name. Also, a Java source file can only have one package statement and unlimited import statements. The package statement (if any) must be the primary (non-comment) line during a source file and the import statements (if any) must come after the package and before the category declaration.
Identifiers Declaration:
Identifiers in Java can begin with a letter, an underscore, or a currency character. Other types of naming are not allowed. They can be of any length. Only in the case of JavaBeans methods, they must be named using CamelCase, and counting on the method’s purpose, must start with set, get, is, add, or remove. In Java, we have variables, methods, classes, packages, and interfaces as identifiers.
Local Variables:
The scope of local variables will be only within the given method or class. These variables should be initialized during declaration. Access modifiers cannot be applied to local variables. A local variable declaration will be as shown below:
public static void main(String[] args) { String helloMessage; helloMessage = “Hello, World!”; System.out.println(helloMessage); } |
Here String helloMessage; is a local variable declaration and its initialization is followed in the next line.
Instance Variables:
Instance variables are values that can be defined inside the class but outside the methods and begin to live when the class is defined.Here, unlike local variables, we don’t need to assign initial values.
- It can be defined as public, private, and protected.
- It can be defined as a final.
- It cannot be defined as abstract and static.
An example can be shown as below:
class Page { public String pageName; // instance variable with public access private int pageNumber; // instance variable with private access } |
Here the declaration String pageName is an instance variable.
Constructors:
We use the constructor to create new objects. Each class is built by the compiler, even if we do not create a constructor defined in itself. constructors can take arguments, including methods and variable arguments. They must have the same name as the name of the class in which it is defined. They can be defined as public, protected, or private. Static cannot be defined because it has a responsibility to create objects. Since it cannot be overridden, it cannot be defined as final and abstract. When the constructor is overloaded, the compiler does not define the default constructor, so we have to define it. The constructor creation order is from bottom to top in the inheritance tree.
Static:
It allows invoking the variable and method that it defines without the need for any object. Abstract and static cannot be defined together, because the method presented as static can be called without creating objects and by giving parameters directly. The abstract is called to override a method. Abstract and static cannot be used together because static has different purposes in this respect.
ENUM:
It is a structure that allows a variable to be constrained to be predefined by one value. With Enum’s getValues method, we can reach all values of enums. This is the most effective way to define and use constants in our Java program.
Features of Java Class Modifiers (non-access):
- Classes can be defined as final, abstract, or strictfp.
- Classes cannot be defined as both final and abstract.
- Subclasses of the final classes cannot be created.
- Instances of abstract classes are not created.
- Even if there is one abstract method in a class, this class should also be defined as abstract.
- The abstract class can contain both the non-abstract method and abstract method, or it may not contain any abstract method.
- All abstract methods should be overridden by the first concrete (non-abstract) class that extends the abstract class.
Java Class Access Modifiers:
Access modifiers are an important part of a declaration that can be accessed outside the class or package in which it is made. Access modifiers enable you to decide whether a declaration is limited to a particular class, a class including its subclasses, a package, or if it is freely accessible. Java language has four access modifiers: public, protected, and private.
- Public Enables a class or interfaces to be located outside of its package. It also permits a variable, method, or constructor to be located anywhere its class may be accessed.
- Protected: Enables a variable, method, or constructor to be accessed by classes or interfaces of the same package or by subclasses of the class in which it is declared.
- Private: Prevents a variable, method, or constructor from being accessed only from within the class in which it is declared.
- Default: The default access occurs when none of the above access specifiers are specified. In such a case, the member is accessible within the package but not without the Java package.
End Note:
In this blog, we talked about declarations of variables, constructors, and Java class modifiers. We also looked at the features of class modifiers and their types.Do you want to know more about topics like Java collections, Java streams, Java Inner Classes, and many more aspects of Java? For in-depth information on these topics, you can check out our series of blogs here.
Get one step closer to your dream job!
Prepare for your Java programming interview with Core Java Questions You’ll Most Likely Be Asked. This book has a whopping 290 technical interview questions on Java Collections: List, queues, stacks, and Declarations and access control in Java, Exceptional Handling, Wrapper class, Java Operators, Java 8 and Java 9, etc. and 77 behavioral interview questions crafted by industry experts and interviewers from various interview panels.
We’ve updated our blogs to reflect the latest changes in Java technologies.
This blog was previously uploaded on March 29th, 2020, and has been updated on January 5th, 2022.
Share