Home Answers Viewqa Java-Beginners Static keyword in public static void main

 
 


sundeep rohilla
Static keyword in public static void main
2 Answer(s)      a year and 5 months ago
Posted in : Java Beginners

Hi,

I have seen that on page mentioned below for static keyword explanation given is: Page: http://www.roseindia.net/java/learn-java-in-a-day/create-first-program.shtml Explanation: static keyword indicates that this method can be invoked simply by using the name of the class without creating any class-object.

Can you I get and an simple example for this?

View Answers

December 23, 2011 at 4:13 PM


The static variable is declared by using static keyword. Following is an example of static declaration:

static int i=10;

The variable declared with static keyword is called class variable. This variable is shared by all the instances of the class. The static variable can be accessed without creating the instance of the class or you can say, it can be called without any reference.

This variable is created statically and same variable is accessed by all instance of the class.

Example of static keyword:

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);
}
}

In the above code, 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


December 23, 2011 at 4:13 PM


The static variable is declared by using static keyword. Following is an example of static declaration:

static int i=10;

The variable declared with static keyword is called class variable. This variable is shared by all the instances of the class. The static variable can be accessed without creating the instance of the class or you can say, it can be called without any reference.

This variable is created statically and same variable is accessed by all instance of the class.

Example of static keyword:

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);
}
}

In the above code, 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









Related Pages:
Static keyword in public static void main
Static keyword in public static void main  Hi, I have seen that on page mentioned below for static keyword explanation given is: Page: http... = a * 4; } public static void main(String args[]) { meth(42); } } In the above
public static void main
public static void main  what is mean public static void main?  ... information, visit the following link: Understanding public static void main.... static-It indicates that the main() method is a class method. void- It indicates
Understanding public static void main function
Understanding public static void main function       The public static void main function is important.... Read more about public static void main function at http://www.roseindia.net
STATIC
is an example: public class StaticTest { static{ System.out.println("Hello from static block"); } public static void helloMethod() { System.out.println("Hello from static method"); } public static void main(String[] args) { helloMethod
Why we are writting public static void main(String args[])?
Why we are writting public static void main(String args[])?  Why we are writting public static void main(String args[])? please elaborate it... and also explain the statement System.out.println("abc
static
StaticClass1 { public static void main(String[] args){ StaticClass1 sv1...redeclare static variable in the inherited class  can we redeclare static variable in the inherited class like this: public class StaticClass1
JAVA what is different between static block and public static void main(String a[]) method
JAVA what is different between static block and public static void main(String... void main(String a[]) method,we execute method without main method(by static block) why need of public static void main(String [])?   Static blocks
static Java Keyword
of the use of the " public static void main method()" in most of the console... getObjects(){ } public static void main method(String av... static keyword with a class: public class Class1
static
static  what r the main uses of static in java   Hi Friend, The Static means that a certain object/variable is resident in memory... instances of the class. When static is applied to methods, the static keyword
What is Public static void main
What is Public static void main In most of the Programming language, main... are the variation of main method. public static void main(String[] args) public static void main(String... args) public static void main(String args
What happen when we write static public void instead of public static void?
What happen when we write static public void instead of public static void?  Hi, What happen when we write static public void instead of public.../gettingstartedwithjava/public-static-void-main.shtml   Hi
Why is the main method declared static?
Why is the main method declared static?  Hi, Why is the main method declared static? thanks   Hi, When we declare main() method in any Java class always has the same signature, and we declare public static void main
static functions
(){ System.out.println("it is static methode"+a); } } class main{ public static void...{ public static void main(string args[]){ a obj=new a();//can ,t requirement..."+a); } } class main{ public static void main(string args[]){ a obj=new a();//can ,t
Can we replace main() with static block or with other static methods?
Can we replace main() with static block or with other static methods?  what is the use of public static void main()?can't we achieve the same thing through static block or through other static methods
static
is why if you declare the function main as static then java does not have...Static Concept  in what are all the situation we may use static...,then use static blocks and without creation of object we need to perform some task
Static variable in java
; } public static void main(String args[]) { salary=10000...Static variable in java. Static is a keyword in java used to create static...; Classname.variablename; Example: Using Static Variable. public class Staticvariable
making consistency of data member in the whole programme without using static keyword
Jointdetails { public: Jointdetails(void); ~Jointdetails(void); bool... { public: Analyzer(void); Jointdetails* GetJointDetails(); Jointdetails... #include "Analyzer.h" class Tst1 { public: Tst1(void); Analyzer *analyzer1
static keyword
static keyword  please give some detail about Static keyword.   Static Variables Static variables are class variables that are shared... and not to object(instance). 2)Static variables are initialized only once , at the start
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
static & instance - Java Beginners
is: " + count); // } public static void main(String[] args... and when can use instance variable.    public class Main { private static int noOfObjects; private int count; public Main
class static - Java Beginners
"); } public static void main(String[] args) { //i=100; j...; public static void staticMethod(){ System.out.println("you can access a static method this way"); } public void nonStaticMethod(){ i=100
Accessing non-static members through the main method in Java.
it is permanently public static void... Accessing non-static members through the main method in Java.  As an oop rule, a static method can have access only to static variables and static
static keyword with real example
static keyword with real example  static keyword with real examplestrong text
why using static keyword
why using static keyword  why using static keyword
core java, static - Java Beginners
"); } public static void main(String args[]) { Static st1 = new Static.... //Static.java public class Static { static void stat() { Static st1... ( Ultimately this is what we do in static main method). So is right to say
static keyword in interface
static keyword in interface  Could any one explain,why static keyword implict (or explict ) come in interface? I asked lot of persons about this question, but nobody did not answered clearly. please explain clearly and say what
non static variable cannot be referenced from static context
public static void main(String []s){ Add a1=new Add(); a1.add(7,8...non static variable cannot be referenced from static context  public class Add{ int foo=7; int bar=8; public int add(int x,int y
non static variable cannot be referenced from static context
public static void main(String []s){ Add a1=new Add(); a1.add(7,8...non static variable cannot be referenced from static context  public class Add{ int foo=7; int bar=8; public int add(int x,int y
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
The void keyword
is the syntax that displays how to use the keyword voidpublic class... The void keyword       The void is a keyword defined in the java programming language. Keywords
Return keyword
of return keyword import java.util.*; public class ReverseString{ public static String reverse(String st){ String rev = new StringBuffer(st).reverse().toString(); return rev; } public static void main(String
I'm getting an illgal start of expression error in my code for the public static boolean portion at the bottom... any ideas?
{ public static void main (String[] args) { Random rand = new Random... { public static void main (String[] args) { Random rand = new Random...; } } } } public static boolean playAgain
Spting AOP Static Pointcut
org.springframework.aop.support.Pointcuts; public class TestPointCut { public static void main...; public class SimpleBean { public void greetMethod() { System.out.println("Have... .style1 { background-color: #FFFFCC; } Static Pointcut Static
static code problem - Java Interview Questions
class AB { public static void main(String args[]) { method(); method(8); } public static void method() { System.out.println("Hello"); } public static void method(int i) { System.out.println(i); } } Thanks
Static
Static  Can i know the real time example for Static method and final variables with defenition? Thank you
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
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
Static Variable
. When static is applied to methods, the static keyword means that the specified...Static Variable  What is the basic use of introducing static variable type in java?Please explain clearly....   The Static means
Static Nested Classes
class having keyword static in the outer class. class OuterClass... Static Nested Classes       A nested class that is declared static is called a static nested class
Calling Static Methods Using SpEL
static void main( String[] args ){ ApplicationContext appContext = new... MyClass { private String random; public void setRandom(String random...Calling Static Method Using SpEL Spring 3 provides powerful Expression
Access Static Member Of The Class Through Object
; public static void main(String[] args) throws IOException {  ....   public static void staticDisplay() {   ...; public void nonStaticDisplay() {    System.out.println
main function
main function  Give me the description of public static void main(Strings args
Static final variables - Java Beginners
Xyz{ private static final String name="xyz"; public void displayData... the variable 'name' } } class Abc extends Xyz{ public void display...Static final variables  can a static final variable be accessed
Example of static method
methods. Static methods can't use any instance variables. The this keyword can't... Example of static method       This Java programming example will teach you the way to define a static
main method
in paranthesis denote?   Hi Friend, public-It indicates that the main() method can be called by any object. static-It indicates that the main() method is a class method. void- It indicates that the main() method has no return value
The byte Keyword
;; Here is an example: public class Main { public static void main... The byte Keyword       The byte Java Keyword defines the 8-bit integer primitive type. 
The final Keyword
MyFinalClass {   public final void myFinalMethod... The final Keyword       The final is a keyword. This is similar to const keyword in other
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
static object array
static object array  static object array   Dynamically call a static variable array class Blog { public static $template = array('content' => 'doodle'); } Blog::$template['content'] = 'bubble'; $class = 'Blog

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.