Question:
how the restrictions of static are not applied on main() function in java programming?
2011-06-11 04:58:50 UTC
methods declared as static have several restrictions out of which few are following:
1. They can only call other static methods.
2. They must only access static data.

my question is that since main() is also preceded with "static" then how main() can call member function of class which are not static.

eg:
class abc
{
int a;
abc(int i)
{
a=i;
}
void dis()
{
System.out.println("a is : " + a);
}
}
class fin{
public static void main(String args[])
{
abc obj = new abc(5);
obj.dis();
}
}

This program will display the output as: a is : 5
Four answers:
2011-06-11 08:32:34 UTC
somdutt bhai, let me explain u something.

when any member(data member or methods) of a class are accessed outside that class, it requires an object reference bcz non-static data members are allocated memory when any object of the relevant class is created. but static members are allocated memory as soon as the class is created. Therefore, they are considered as class variables. the static members need not any object reference, they can be always called directly by the class itself.

morever, when a static method is called, it doesnt have any object refernce( bcz it has been called directly by class), so it can not call any non-static member directely(bcz non-static members require an object reference). but even though using any other object reference, it can call non-static members also.

lets see ur first program-



class abc

{

int a;

abc(int i)

{

a=i;

}

void dis()

{

System.out.println("a is : " + a);

}

}

class fin{

public static void main(String args[])

{

abc obj = new abc(5);

obj.dis();

}

}



first main() method is called by java run time system without using any object( called directly by class, thats why we have to qualify main() as static). now main() cant call dis() function directly bcz it is non static(it requires an object reference,which main() doesnt have). so it has to create an object of abc class and call dis() using that object.

remember, if dis() had been static, it could have been called by main() like-

abc.dis();

the same explanation is continued for ur second program except that if dis() had been static, it could have been called by main() like-

dis();
green meklar
2011-06-11 17:20:14 UTC
A static method is only not allowed to make 'naked' calls to nonstatic methods. Getting an object of the same class (or any other instantiable class) inside the static method and calling a nonstatic method on that is no problem at all. This is true both for main() methods and other static methods. In your examples, you are constructing an object inside the method and calling that object's nonstatic method. That's fine. It's only if you tried to call dis() by itself that you would have a problem due to dis() being nonstatic.



For instance (no pun intended), look at this simple code:



public class abc

{

public void blah()

{

}

public void zxcv()

{

blah(); //line A

}

public static void qwer()

{

abc whatever=new abc();

whatever.blah(); //line B

blah(); //line C

}

}



If you tried to compile that, it would fail, but you would notice that it would only fail on line C, with an error type along the lines of 'cannot make a static reference to the non-static method blah()'. It would not give the same error for lines A or B, and in fact, if you deleted or commented out line C, it would then compile with no errors. The reason line A is not a problem is that, although it is calling a nonstatic method, the call is being made inside a nonstatic method, so the program will already know which instance is implicitly being used whenever zxcv() is called and will call blah() on that same instance. The reason line B is not a problem is that, although it is calling a nonstatic method from a static method, it is explicitly specifying to call it on a specific instance (the instance we called whatever and constructed inside the qwer() method). Only line C fails, because it is calling a nonstatic method, but the call is being made from a static method and the instance to call the nonstatic method on has not been specified.
CHINMAY PEDNEKAR
2011-06-11 12:03:19 UTC
That is because it is calling non static members of another class

The restrictions should be

1. They can only call other static methods OF SAME CLASS

2. They must only access static data OF SAME CLASS



If you have

class fin{

int a;

public static void main(String args[])

{

a=5;

System.out.println(a);

}

}

This won't work. for this to work, a needs to be static







class fin{

static int a;

public static void main(String args[])

{

a=5;

System.out.println(a);

}

}

This will work



in the above program, you can observe that to access the non static data and members of class abc, main() had to create object of class abc. Had it been a non static member, then the calling procedure is

classname.membername(). eg: Math.sqrt()
Mike T
2011-06-11 15:14:49 UTC
Neither statement of the restrictions of static is stated quite right.



1. They can only call other static methods of the same class, unless they are referencing an instance of the class.



public class Foo {

   public static final main(String argv[]) {

      Foo anInstance = new Foo();

      anInstance.doIt();

   }



   private void doIt() {

      // do the real work here

   }

}



2. They can only access static data. This has the same restrictions as accessing instance methods.



The best way to think of the restrictions on static methods is to think about how they differ from instance methods. When you write something like this:



class Bar {

   int count = 0



   void increment() {

      count++;

   }



   private void doSomethingSpecial() {

      // useful code here

   }



   void decrement() {

      count--;

      doSomethingUseful();

   }

}



The use of the variable count in the method increment is a shorthand way of saying this.count++.

Likewise, the call to doSomethingUseful in decrement is a shorthand for this.doSomethingUseful().



Static methods differ from instance methods in that the implictly defined pseudo-variable "this" is not available. Therefore, your restriction for static methods is that you can't write code that needs to explicitly, or implicitly, reference the pseudo-variable this.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...