Use of Static Keyword in a Class

0 votes

I was trying to perform the following operation:

package hello;

public class Hello {

    Clock clock = new Clock();

    public static void main(String args[]) {
        clock.sayTime();
    }
}

But it gave the error

Cannot access non-static field in static method main

So I changed the declaration of clock to this:

static Clock clock = new Clock();

And it worked. What does it mean to put that keyword before the declaration? What exactly will it do and/or restrict in terms of what can be done to that object?

May 29, 2018 in Java by developer_1
• 3,320 points
447 views

1 answer to this question.

0 votes

It means that there is only one instance of "clock" in Hello, not one per each separate instance of the "Hello" class, or more-so, it means that there will be one commonly shared "clock" reference among all instances of the "Hello" class.

So if you were to do a "new Hello" anywhere in your code: A- in the first scenario (before the change, without using "static"), it would make a new clock every time a "new Hello" is called, but B- in the second scenario (after the change, using "static"), every "new Hello" instance would still share and use the initial and same "clock" reference first created.

Unless you needed "clock" somewhere outside of main, this would work just as well:

package hello;
public class Hello
{
    public static void main(String args[])
    {
      Clock clock=new Clock();
      clock.sayTime();    
    }
}
answered May 29, 2018 by Rishabh
• 3,620 points

Related Questions In Java

+2 votes
3 answers

Listing all the subclasses of a specific class in Java

Try with BurningWave core library. Here an ...READ MORE

answered Dec 29, 2019 in Java by anonymous
• 460 points

edited Jan 9, 2020 by anonymous 12,918 views
0 votes
2 answers

What is the use of final class in java?

In Java, items with the final modifier cannot be ...READ MORE

answered Oct 5, 2018 in Java by Daisy
• 8,120 points
1,572 views
0 votes
1 answer

What is the use of assert keyword in java?

Java assertion feature allows the developer to ...READ MORE

answered Jun 28, 2018 in Java by scarlett
• 1,290 points
555 views
0 votes
2 answers

Why are you not able to declare a class as static in Java?

A static keyword  can be used with ...READ MORE

answered Jun 11, 2019 in Java by Neha
• 330 points
1,107 views
0 votes
2 answers

Ho do I Iterate through a HashMap which contains duplicate values

for (Map.Entry<String, String> item : params.entrySet()) { ...READ MORE

answered Jul 24, 2018 in Java by samarth295
• 2,220 points
4,789 views
0 votes
2 answers

Performing Iteration over each entry in a 'Map'

In Java 8 you can do it ...READ MORE

answered Oct 24, 2018 in Java by Sushmita
• 6,910 points
776 views
0 votes
2 answers
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,370 points
675 views
0 votes
2 answers

Counting no of Occurrence of a particular character inside a string in Java

We can find out the no. of ...READ MORE

answered Sep 7, 2018 in Java by Sushmita
• 6,910 points
2,275 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,120 points
3,756 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP