Static Function in Java27 Mar 2025 | 3 min read In Java, the static keyword can be used with variable, constant, and functions. The main purpose of using the static keyword is to manage the memory so that we can use the memory efficiently. In this section, we will discuss the static function in Java. Static FunctionIf the keyword static is prefixed before the function name, the function is called a static function. It is often called a method. A method is a group of variables and statements that functions together as a logical unit. Like fields, methods can have modifiers (like private, public, or static). Methods also have a return type (may be void if the method returns nothing). The return type can be a reference type (such as an object or an array). A method can have any number of parameters or arguments. When a function is static, it is really part of the class and not part of the individual objects in the class. It means that static functions exist even before creating any objects. The best example of a static method is the main() method. Properties of Static Function
Declaring a Static FunctionThe declaration of a static function in Java is the same as the declaration of a method. The function has two parts function prototype and body of the function. ![]() The function prototype contains the function signature that includes the function name, return type, access specifier, and the parameters list. The function body contains the logic or functionality to be performed. Syntax: In the above syntax, the access specifier and the parameter list are optional. A function may or may not have a parameter list. For example: Calling Static FunctionIn Java, we cannot call the static function by using the object. It is invoked by using the class name. For example: Example of Static Function in JavaLet's create a Java program from which we can clearly understand the difference between static and non-static functions. StatciFunctionExample.java Output: A non-static function is called. The static function is called. Let's create a Java program and call a static method from another static method even without creating an object of the class. CalculateCube.java Output: Cube of 7 is: 343 Restrictions of the Static MethodThere are two main restrictions for the static method. They are:
Demo.java Output: ![]() Next TopicImageicon-class-java |
Binary tree is a tree type non-linear data structure that are mainly used for sorting and searching because they store data in hierarchical form. In this section, we will learn the implementation of binary tree data structure in Java. Also, provides a short description of...
64 min read
In Java, the TreeMap class is a commonly used implementation of the Map interface that stores key-value pairs in a sorted order based on the natural ordering of its keys or a custom comparator. By default, TreeMap sorts the elements by keys in ascending order. However,...
5 min read
The Sieve of Eratosthenes is one of the most efficient algorithms for identifying all prime numbers up to a given number, the limit. The above-stated procedure is named after the ancient Greek mathematician Eratosthenes, who developed this intelligent technique. It operates on a simple principle: each...
5 min read
One fascinating difficulty in many string-related problems is locating and counting substrings that meet specific requirements. Finding and counting all substrings with at least one vowel and one consonant is one such task. This problem illustrates a variety of algorithmic techniques, from brute force tactics...
8 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Flipkart etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to...
4 min read
Abstract Syntax Tree is a computer language's abstract syntactic structure is represented by a type of tree called a tree. A construct that is present in the source code is indicated by each node of the tree. Typically, an AST is the output of a compiler's syntax...
3 min read
Java, a versatile and widely used programming language, is renowned for its object-oriented approach and platform independence. One of the key features contributing to the language's organizational prowess is the concept of packages. In Java, packages serve as containers for classes, providing a structured way to...
6 min read
A final variable can be initialized at the time of declaration or in a constructor, but once assigned, it cannot be modified. The final keyword is used to declare constants. Use the final keyword to declare a variable as final. It is treated as constant. Syntax: final...
4 min read
? Understanding the size of data types is crucial for efficient memory management in any programming language. In Java, the size of an int is platform-dependent, which means it can vary across different systems. In this article, we will explore various techniques to determine the size of...
3 min read
Java programming challenges are complex problems that we have to solve through logic and implement that logic in any programming language. Solving programming challenges develops logical thinking, analytical skill. If you are preparing for interviews, you must solve these programming challenges once. It is advisable to...
22 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India