hi :)
I'm wondering >>what is the use of (static) word in func.'s ?
in this code what does it mean ??and if I didn't put it what would happen then ??
thanks :)

import java.util.Scanner;
class Average {
public static double compute(int...x)
double sum=0;
    for(int item:x)

    return sum/x.length;

    public static void main(String[] args) {

        System.out.println("enter 3 no.'s");
        Scanner obj=new Scanner (System.in);
        int no1=obj.nextInt();
        int no2=obj.nextInt();
        int no3=obj.nextInt();

        System.out.println("Average is =>"+ compute(no1,no2,no3));



Recommended Answers

All 11 Replies

It means the method is a class method as opposed to an instance method. You do not need an instance of the class to use that method, and the method has no access to any instance variables or instance methods )unless someone else supplies the instance).

1.You would get an error saying that there is no main type when you try to run removing static from the main() .
2.If you remove static from compute() and not the main() then you may not access it from main() as its a static method. You will get an error like "Cannot make a static reference to the non-static method ".
Note: Static methods belong to the class and dont need to be instantiated(you may instantiate but you would get a warning).

newcoder: "you may instantiate but you would get a warning" ... not really. it would run perfectly, but chances are, you would confuse a developer reading your code, thinking he's working with instance methods and thus changing values for only a certain instance, not for all of them.

yes, really.
NetBeans and Eclipse both give a compile-time warning for accessing a static method via an instance ref. It is, as you say, valid but confusing.

James: and what if you compile by command prompt, instead of an IDE? I agree, Netbeans and Eclipse 'll tell you something is wrong, but saying that 'by default' every time someone compiles something like this, he is getting a compilation error (message) is wrong.

if he had said "when you use Netbeans or Eclipse", I would have agreed.

Compiling from the command prompt gives you a "warning: [static] static method should be qualified by type name, A, instead of by an expression" (provided you are using -Xlint, which, frankly, you would be mad not to).

// demo code
class A {
  static void x() {}
  {new A().x();}

James: just to be sure ... I tested it here, just to be sure myself.
I compiled code both in Windows 8 and in Ubuntu, where I called a static method through an instance. No warning, no error, code compiles cleanly and is ready to run.

Mine was Java 8 on Yosemite
did you use -Xlint?

nope. a simple
javac Hello.java

OK. That explains it.

For some bizarre reason you have to ask the compiler to check your code properly by adding the -Xlint option. IMHO that should have been the default, with an option to turn it off if you need to (like -nowarn).
Anyway, I cannot understand why anyone would NOT automatically always use -Xlint when developing a program - it's on a par with catch(...) {} in terms of ignoring the diagnostics that Java works so hard to provide.

The moral (for anyone else reading this) - always compile with -Xlint unless you have a good reason not to!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.