Home Tutorialhelp Comment Static Keyword in java

 
 

Comment

manoj kumar yadav
Static Keyword in java,
January 29, 2008 at 12:01 PM

There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static


because it must be called before any objects exist.
Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.

Methods declared as static have several restrictions:

They can only call other static methods.
They must only access static data.
They cannot refer to this or super in any way. (The keyword super relates to
inheritance.)
If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:

// Demonstrate static variables, methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}

As soon as the UseStatic class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized to a * 4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x. The three println( ) statements refer to the two static variables a and b, as well as to the local variable x.

Note It is illegal to refer to any instance variables inside of a static method. Here is the output of the program:

Static block initialized.
x = 42
a = 3
b = 12

Outside of the class in which they are defined, static methods and variables can be used independently of any object. To do so, you need only specify the name of their class followed by the dot operator. For example, if you wish to call a static method from outside its class, you can do so using the following general form:

classname.method( )

Here, classname is the name of the class in which the static method is declared. As you can see, this format is similar to that used to call non-static methods through object reference variables. A static variable can be accessed in the same way—by use of the dot operator on the name of the class. This is how Java implements a controlled version of global functions and global variables.
Here is an example. Inside main( ), the static method callme( ) and the static variable b are accessed outside of their class.

class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
Here is the output of this program:
a = 42
b = 99


View All Comments | View Tutorial
Advertisement
Related Tutorial and Articles
Java Beginners

Static keyword in public static void main
://www.roseindia.net/java/learn-java-in-a-day/create-first-program.shtml Explanation...; The variable declared with static keyword is called class variable. This variable... by all instance of the class. Example of static keyword: class UseStatic 

General

static Java Keyword
static Java Keyword       The static is a keyword defined in the java programming language. Keywords... in java programming language likewise the static keyword indicates the following 

General Information

static keyword in java
.style1 { color: #0000FF; } static keyword in java We are going to discuss about static keyword in java. The static keyword is a special keyword in java programming language. A static member belongs to a class 

Java Interview Questions

static keyword
static keyword  Hii, In which portion of memory static variables stored in java. Is it take memory at compile time? thanks deepak mishra 

Java Beginners

static
instances of the class. When static is applied to methods, the static keyword...static  what r the main uses of static in java   Hi Friend... and accessed each time an instance of a class, an object, is created. When static 

Java Beginners

static
instance of that object (the object that is declared static) in the class... of the class that is initialized. Another thing about static objects is that you... is why if you declare the function main as static then java does not have 

Java Beginners

Day for the given Date in Java
java.util.*; import java.text.*; class CheckDay{ public static void main...Day for the given Date in Java  How can i get the day for the user..."); String day=f.format(date); System.out.println(day 

Java Tips and Examples

Learn Java in a day
Learn Java in a day   ... structure. Initially learn to develop a class in java. To meet this purpose we..., "Learn java in a day" tutorial will definitely create your 

Java Beginners

class static - Java Beginners
class static  When do we declare a member of a class static?  Hi friend, public class HowToAccessStaticMethod{ int i; static int j... information on Static in Java visit to : http://www.roseindia.net/java/beginners 

General

The new keyword in java
The new keyword in java In this section you will learn about new keyword in java. The new keyword is used to create a instance of a class. The new keyword... to be remember : The  new  keyword is followed by a class name 

Core Java

Static variable in java
Static variable in java. Static is a keyword in java used to create static methods, variable inside a class and static class. Static variable is also called class variable which belongs to class not to object. Static 

General

Java get Next Day
Java get Next Day       In this section, you will study how to get the next day in java...*; public class GetNextDay {     public static void main(String[] args 

Java-Tutorials

Transient Java Keyword
Transient Java Keyword In this section we will discuss about transient keyword in java. Transient is a keyword in java which is used to prevent any variable... to learn about serialization in java. Serialization is used to save the state 

General

The extends keyword
The extends keyword In this section you will learn about extends keyword in java. The extends is a keyword in java which is used in inheritance. It is used to specify super class in a class declaration. Using this keyword a subclass 

Java Beginners

Return keyword
Return keyword  how do we use return in methods?   Java use of return keyword import java.util.*; public class ReverseString{ public static String reverse(String st){ String rev = new StringBuffer(st 

Java Beginners

Static Variable
type in java?Please explain clearly....   The Static means... of a class, an object, is created. When static is applied to member variables, it means that the static variable is shared among all instances of the class 

Java Beginners

Java inner class and static nested class
Java inner class and static nested class  Java inner class and static nested class 

Java Interview Questions

static
static  What is the exact need of declaring a variable as static?   A static variable is a variable who's single copy in memory is shared by all objects,so any modifications to the static variable will modify it's value 

Java Tips and Examples

How to use this keyword in java
How to use "this" keyword in java       The keyword this is useful when you need to refer to instance of the class from its method. The keyword helps us to avoid name 

Java Interview Questions

Java keyword
Java keyword  Is sizeof a keyword 

Core Java

synchronized Java Keyword
synchronized Java Keyword       The synchronized is a keyword defined in the java programming... relevant to a compiler in java programming language likewise the synchronized keyword 

Core Java

this java keyword
this java keyword       "this" is a keyword defined in the java programming language. Keywords... in java programming language likewise the this keyword indicates the following 

Core Java

Keyword - this
Keyword - this       A keyword is a word having a particular meaning to the programming language. Similarly, this keyword is used to represent an object constructed from a class 

Java Beginners

static keyword
static keyword  please give some detail about Static keyword.   Static Variables Static variables are class variables that are shared... instances of the class. 4)A static variable can be accessed directly by the class name 

Core Java

The final Keyword
a variable or class with this name in your Java program. In the Java programming... The final Keyword       The final is a keyword. This is similar to const keyword in other 

General

Super keyword in java
Super keyword in java In this section we will discuss about the super keyword in java. Super is a keyword defined in java. Super is used to refer... "super" keyword. Here is the example to use super to call super class 

Core Java

The for Keyword
The for Keyword       The for is Java keyword that is used to execute a block of code... Java program. You can also use this keyword for nested loop i.e. loop within loop 

Java Tips and Examples

Example of static method
; This Java programming example will teach you the way to define a static methods. In java we have two types of methods, instance methods and static methods. Static methods can't use any instance variables. The this keyword can't 

Core Java

Java final Keyword Example
classes) In Java class can be declared as final using final keyword...Java final Keyword Example In this section we will read about the final keyword in Java in various context. final keyword in Java can be used 

Java Code Examples

Java date add day
Java date add day In this tutorial, you will learn how to add days to date... to the calendar and using the Date class, we have got the date of that day. Example import java.util.*; public class AddDaysToDate{ public static void main 

Core Java

The if Keyword
is executed. The if is a java keyword that may not be used as identifiers i.e. you cannot declare a variable or class with this name in your Java program... The if Keyword         

Programming Tutorials

Implementation of Day Class
to be returned is Monday. Here is the code: public class Day { final static int...Implementation of Day Class Here we are going to design and implement the class Day. The class Day should store the days of the week. Then test the following 

Java-Tutorials

Learn Java in 21 days
of the class, method, keyword, function and other important parts of a Java...Anyone can learn Java in 21 days, all they need is dedication and concentration. Java Guide at RoseIndia can help you learn Java in 21 days. The course 

General

The byte Keyword
; The byte Java Keyword defines the 8-bit integer primitive type.  The keyword byte in Java is a primitive type that designates with eight bit signed integer  in java primitive type. In java keyword byte will be stored as an integer 

Java Technology Tutorials

The double Keyword
; The double is a Java keyword that may not be used as identifiers i.e. you cannot declare a variable or class with this name in your Java program. It is used... is generally the default choice. The Java wrapper class associated 

General

Static Nested Classes
class having keyword static in the outer class. class OuterClass...; A nested class that is declared static is called a static nested class... independently of any particular outer class object. A static nested class use 

General

The float Keyword
The float Keyword         The float is a Java keyword that may not be used as identifiers i.e. you cannot declare a variable or class with this name in your Java program 

Core Java

The interface keyword
; In java programming language the keyword interface in java is used... to implement an interface by a class. Interfaces in java are abstract means they can...). An abstract type in java defines that it must be implemented by any class to make 

Core Java

Static
an instance of a class. Static methods are implicitly final method, since overriding depends on the type of the object, and static methods are attached to a class.../struts/struts2/strutsresources/static-parameter.shtml http://www.roseindia.net/java 

Java Beginners

Output Previous or Next Day of the Month
Output Previous or Next Day of the Month   Please Help! I need to create the following program: Objective of this Program: (1) To learn about having... the if...then...else construct and (2) to start using some of the Java API's, by using the String 

Core Java

while Java Keyword
while Java Keyword       The while is a keyword defined in the java programming language... to a compiler in java programming language likewise the while keyword indicates 

Core Java

true Java Keyword
true Java Keyword       The true is a keyword defined in the java programming language. Keywords... in java programming language likewise the true keyword indicates the following 

General

volatile Java Keyword
volatile Java Keyword       The volatile is a keyword defined in the java programming language... to a compiler in java programming language likewise the volatile keyword indicates 

General

The extends Keyword
; The extends is a Java keyword, which is used in inheritance process of Java. It specifies the superclass in a class declaration using extends keyword. It is a keyword that indicates the parent class that a subclass is inheriting from 

Core Java

Java final keyword
Java final keyword The final is a very important keyword in java, which... keyword can be applied to: method class variable Syntax of final class...: The final keyword applied to a class, indicating the class cannot 

Core Java

void Java Keyword
void Java Keyword       The void is a keyword defined in the java programming language. Keywords... in java programming language likewise the void keyword indicates the following 

Flex Questions

Dynamic keyword
during the run-time. Just add the magic keyword dynamic to the class definition...Dynamic keyword  hi....... What is the dynamic keyword used for in flex? give me the answer ASAP Thanks  Ans: Dynamic keyword 

General

transient Java Keyword
transient Java Keyword       The transient is a keyword defined in the java programming language... to a compiler in java programming language likewise the transient keyword indicates 

Java Beginners

core java, static - Java Beginners
core java, static  Dear sir, in java non-static fields can not be accessed in a static field. But if we create an object of class type in static.... //Static.java public class Static { static void stat() { Static st1 

Java Beginners

static in java
static in java  what is the need to go static? what use 

Java Beginners

static functions
Java Static Method   class a{ static int a=1; static void display...)It is a variable which belongs to the class and not to object(instance). 2)Static... variables. 3)A single copy to be shared by all instances of the class. 4)A static 

Java Beginners

static & instance - Java Beginners
static & instance  what is different between static variable and instance variable .? plz explain with example. and when we can use static variable and when can use instance variable.    public class Main 

Core Java

The implement keyword
; In java programming language, the keyword implement specifies... for using the implement keyword in a class. public class DominoPizza...) into a java class. A class implementing an interface must either implement all 

Core Java

throws Java Keyword
throws Java Keyword       throws " is a keyword defined in the java programming language. Keywords... in java programming language likewise the throw keyword indicates the following 

General

super Java Keyword
super Java Keyword       The super is a keyword defined in the java programming language... to a compiler in java programming language likewise the super keyword indicates 

Java-Tutorials

Learn Java for beginners
of their home and become a Java professional in no time. To learn Java, a beginner has..., class, keyword, etc. and implementing them in programs and examples. This helps... on expanding and adding new codes every day, this creates a problem for Java 

Core Java

The While keyword
; While is a keyword defined in the java programming language. Keywords... : The while keyword in java programming language specifies a loop  The loop... using the keyword while. public class Myclass { while (expression 

Core Java

The void keyword
The void keyword       The void is a keyword defined in the java programming language. Keywords... is the syntax that displays how to use the keyword void.  public class 

General

Java Break keyword
in Java an Example import javax.swing.*; public class Java_Break_keyword... Java Break keyword       Java programming involves loops that are widely used by programmers 

Core Java

Public Java Keyword
Public Java Keyword       public is a keyword defined in the java programming language. Keywords... outside the class in which it is declared.  Using a public Keyword within 

Core Java

Private Java Keyword
Private Java Keyword       private is a keyword defined in the java programming language. Keywords... in java programming language likewise the private keyword indicates the following 

General

Class in Java
structure. First of all learn:  what is a class in java and then move on to its... Class in Java      ...; in java program. import : Import keyword is used at the beginning 

Core Java

throw Java Keyword
throw Java Keyword       "throw " is a keyword defined in the java programming... to a compiler in java programming language likewise the throw keyword indicates 

Java Tips and Examples

Find Day of Month
Find Day of Month       This example explores how to find the day of a month and the day of a week  This example sets the year as 2007 and the day as 181. The example  

Core Java

Return Java Keyword
Return Java Keyword       The return is a keyword defined in the java programming language. Keywords... in java programming language likewise the return keyword indicates the following 

General

Protected Java Keyword
Protected Java Keyword       protected is a keyword defined in the java programming language. Keywords... in java programming language likewise the protected keyword indicates 

Programming Tutorials

The throw keyword example in java
Code: class MyMadeException extends Exception{} class ExceptionExample { public static void main(String[] args) { String test = args[0]; try...;); } System.out.println ("Sixth"); } static void doriskyJob (String t) throws 

Core Java

The volatile keyword
The volatile keyword       The volatile is a keyword defined in the java programming language. Keywords... keyword is not implemented in many Java Virtual Machines. The volatile 

Core Java

short Java Keyword
short Java Keyword       The short is a keyword defined in the java programming language. Keywords... in java programming language likewise the short keyword indicates the following 

Core Java

try Java Keyword
try Java Keyword       The try is a keyword defined in the java programming language. Keywords... : -- The try keyword in java programming language is used to wrap the code in a block 

Java Tips and Examples

Learn Java - Learn Java Quickly
Learn Java - Learn Java Quickly   ... by the Java compiler into byte code that are stored in .class file. The JVM..., TextPad etc. Java program is a class with a main method in it. The main method 

Core Java

switch Java Keyword
. Example to use the Switch keyword within a class in java programming language... switch Java Keyword       The switch is a keyword defined in the java programming language. Keywords 

Java-Tutorials

Easiest way to learn Java
There are various ways to learn Java language but the easiest way to learn..., video tutorials and lectures that help beginners learn in Java. This tutorials..., Java classes, etc are covered. To make it easy for novices to learn 

Java Beginners

transient keyword - Java Beginners
transient keyword  what is transient keyword in java.? plz explain with example 

Programming Tutorials

Java static method example
Description: Static variable are used within the static method. Non static variable do not exits in the static method. Static variables are shared by its class instances. Static variable can be used without using the instances 

Core Java

Super - keyword
; super keyword super is a keyword in java that plays an important role in case of inheritance. Super keyword is used to access the members of the super class. Java... the variables of class A, to access to those variables we the super keyword. Accessing 

General

The new keyword
; The new keyword in java programming language is used to create a new instance... an instance of a class or an array by using the new operator. Creating a simple java... the new keyword must be a class name followed by a series of constructor 

Java Beginners

learn
learn  how to input value in java 

Java Beginners

Static final variables - Java Beginners
Static final variables  can a static final variable be accessed by a instance (non-static)method?  Hi, static final variable u can access in non-static method. but the variable should has accessibility, means 

Java Beginners

final keyword
final keyword  why is final keyword used in java 

Core Java Tutorials

Using throw keyword in exception handling in Core Java
Description: Core Java Throw Function is used for throwing the exception. The throw keyword tells the compiler that it will be handled by calling a method... exception need to be thrown from the calling method. Code for Java Throw Exception 

Java Beginners

about static import - Java Beginners
*; class B { public static void main(String ar[]) { System.out.println(i... is not static. Use public modifier instead of protected modifier in Class...: package mypack; public class TestA { public static int i=100; } 2 

Java Beginners

Hey Guys really need to learn - Java Beginners
u will use scanf(%d%d%d, & month, day, year). How can i do this in java... java.util.*; public class UserInput{ public static void main(String[]args...Hey Guys really need to learn  Im a java beginner and i want to know 

Java Beginners

How we learn java
How we learn java  what is class in java? what is object in java? what is interface in java and use? what is inheritence and its type 

Java Beginners

How we learn java
How we learn java  what is class in java? what is object in java? what is interface in java and use? what is inheritence and its type 

Java Beginners

How we learn java
How we learn java  what is class in java? what is object in java? what is interface in java and use? what is inheritence and its type 

Java Beginners

How we learn java
How we learn java  what is class in java? what is object in java? what is interface in java and use? what is inheritence and its type 

Java Beginners

How we learn java
How we learn java  what is class in java? what is object in java? what is interface in java and use? what is inheritence and its type 

Struts 2 Tutorials

Static Parameter, Static Parameter in Struts 2, Struts 2 Static Parameter
<action name="StaticParameter" class="...;struts2tutorial\WEB-INF\src\java\net\roseindia" directory. Here  the "StaticParameter" class extends the ActionSupport class. It sets and gets 

Applet Questions

learn
learn  i need info about where i type the java's applet and awt programs,and how to compile and run them.please give me answer 

Java-Tutorials

How to learn Java?
, class, function, method, keyword, etc. there is to learn. Every topic... to learn Java language than there are two ways you can do that. One is the classroom... training. A young developer who is seeking a career in Java can learn the language 

Java Beginners

java inner class - Java Beginners
, Differences: 1)Nested class is declared inside another class with the static keyword or within a static context of that class wheras Inner class is a nonstatic class...java inner class  What is the difference between the nested class 

Java Interview Questions

Java static method
Java static method  Can we override static methods 

Java Interview Questions

Java static import
Java static import  What is the use of static import 

General

Get first day of week
Get first day of week       In this section, we will learn how to get the first day of ..._package>java FirstDayOfWeek Day of week: 7 Sunday 

Core Java

The package keyword
The package keyword       The package in java programming language is a keyword that is used to define a package that includes the java classes. Keywords are basically reserved words 

Java Beginners

Java Date Class
class DateClassExample { public static void main(String[] args) { Scanner...Java Date Class  Hi Friend, I need to have a code to get the days..., the days would be saturday, sunday and monday, the code I gave simply get one day 

Java Beginners

Static & Instance variables in java
Static & Instance variables in java  What are the Difference between Static & Instance variables in java 

Java Beginners

Static Method in Java
Static Method in Java  Is Overriding Static methods possible in java? If Yes, How 

Java Tips and Examples

Find the Day of the Week
Find the Day of the Week       This example finds the specified date of  an year and a day of a week from calendar. The Calendar class is an abstract base class that extends 

Advertisements Advertisements
 

 

 

DMCA.com