Understanding Static Keyword.

Hello Friends.....!!
If I declare any method as static can have several restriction as...
1) They can only call other static methods.
2) They must only access static data.
3) They cannot refer to this and super in any way...

(As per book Herbert Schidt page no: 141)
But I wrote a program which is not agreeing with this...Can you please tell me what is static methods and details...I am getting confused..😭😐
The program code is given below..

class operation
{
static int i = 600;
int a,b;
void call(int x, int y)
{
a = x;
b = y;
}
void print()
{
System.out.println("value of a:"+a+ " value of b:" +b);
}
static void display()
{
System.out.println("THis is static method:");
}
}
class test extends operation
{
void dis()
{
System.out.println("value of i: "+i);
}
static void go() //this method access non-static methods and variables as
// it is static
{
display();
operation e = new operation();
e.a = 111;
e.b = 222;
e.print();
}
}
class start extends operation // if I am not extending operation class then it show an
{ // error cannot find display method as the method is static
// I can access it from any where - am I right?
public static void main(String ar[])
{
test v = new test();
v.go();
v.dis();
display();
}
}

I am totally confused about static keyword please help....!😭😭

Replies

  • SheldonCooper
    SheldonCooper
    The book says,"When a member is declared static,it can be accessed before any objects of its class are created,and without reference to any object."
    It do not say "must".

    Then moving towards your problem:
    "If I declare any method as static can have several restriction as...
    1) They can only call other static methods.
    2) They must only access static data.
    3) They cannot refer to this and super in any way."
    It should be taken as 'Directly' i.e.'without creating object'.And if in the same class.

    I have rewritten the 'start' class for you.Copy it into your code and then check.It will explain you the rest.By the way nice question.

    class start extends operation
    static void methodname1()
    {
    System.out.println("Hi");
    }
    void methodname2()
    {
    System.out.println("Hi2");
    }
    public static void main(String ar[])
    {
    methodname1();
    methodname2();//This is invalid.Change it to comment while compiling
    test v = new test();
    v.go();
    v.dis();
    display();
    operation.display();
    operation.print();//This is invalid.Change it to comment while compiling
    operation ob=new operation();
    ob.print();
    ob.display();
    }

    }
  • TheV
    TheV
    Thank you cooper I got you....!!
  • SheldonCooper
    SheldonCooper
    TheV
    Thank you cooper I got you....!!
    😁You are welcome.And that was my first post.😁
  • TheV
    TheV
    Carry on with your helping hands....!!

You are reading an archived discussion.

Related Posts

Lecture 1 & 2 outline the course and importance of basic electrical technology. Prof. L. Umanand explains it. Feel free to ask your questions, doubts, queries in this discussion and...
Professor L. Umanand discusses most primary components in an electric circuit—resistors, capacitors and inductors. Discuss this lecture with fellow electrical & electronic engineering students and share your ideas, views, questions,...
Lecture on electrical sources, and discusses practical, real-world AC and DC sources, including photovoltaics by Prof. L. Umanand of IISc Bangalore Power Electronics Department. Discuss this lecture with fellow electrical...
Prof. L. Umanand discusses Kirchoff's law in depth and explains single & 3-phase wall outlets along with their behavior. Discuss this topic with fellow engineers. Ask your questions, doubts and...
CEans, I looked at our member count today and in next few hours, we'll be 94k strong 👍 . We had discussed about having online celebrations on crossing 100,000 members....