CodeGym /Courses /New Java Syntax /Static classes and methods

Static classes and methods

New Java Syntax
Level 11 , Lesson 3
Available

"In addition to static methods, there are static classes. We'll discuss these in more detail later. For now, let me just show you an example:"

Example:
public class StaticClassExample
{
    private static int catCount = 0;

    public static void main(String[] args) throws Exception
    {
        Cat bella = new Cat("Bella");
        Cat tiger = new Cat("Tiger");

        System.out.println("Cat count " + catCount);
    }

     public static class Cat
    {
        private String name;

        public Cat(String name)
         {
            this.name = name;
            StaticClassExample.catCount++;
         }
     }

}

"You can create as many Cat objects as you want. But this isn't the case with a static variable. Only one copy of a static variable exists."

"The main purpose of using the static modifier in the class declaration is to control the relationship between the Cat and StaticClassExample classes. The idea is roughly this: the Cat class is not linked to StaticClassExample objects and can't access the instance (non-static) variables of the StaticClassExample class."

"So I can create classes inside classes?"

"Yes. Java allows that, but don't give it too much thought right now. It will become clearer when I explain some more things to you in the future."

"I hope so, Rishi."

Comments (25)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoist Level 3, San Diego, United States
19 September 2022
Can someone please further add/clarify to -- Carlos explanation -- below. It's a good explainer but still some healthy debate going on .... Thanks
Aldo Luna Bueno Level 1, Peru
21 November 2021
Static classes is the first new topic for me.
Ashish RajAnand Level 13, Bhilai , India
1 March 2020
i got little thing about static class and method ,but sure about static variables,
Gellert Varga Level 23, Szekesfehervar, Hungary
9 April 2020
I think the static class is the same thing as the static variable or the static method. The essence: Everything that is static, is not part of the instances of StaticClassExample class. The static things belong only to their own class, but not to the objects of the class. In other words: The static things is not part of the pattern that describes the objects.
Ashish RajAnand Level 13, Bhilai , India
12 April 2020
static things also belong to other class too. example static -> main() method . every inbuilt method you call all are static method and you also see in the example how to access the static variable , In future you will find more lesson in this course and information how static method word . Go ahead .
Thomas Sixberry Level 16, Rochester Hills, United States
25 January 2020
I forgot the : .. how do I always forget something in these type what you see things. D:
Anonymous #10428383 Level 12, Seattle, United States
21 January 2020
Hi Joseph, let me try to explain this "out side of water" static method or stuff. e.g: in OOP objects communicates by msg righ! you don't access it until you create one using "new"; storage are allocated and methods and vars.become available. when you do this you are describing how objects of this class look and how they will behave. """"but"""" when you want to have only one single piece of storage for a particular field, or if you need a method that isn't associated with any particular field of this class, no matters if objects of this class have been created or not, you use the "static keyword". e.g class AnyName{ static int i = 40; String name;} you can do this AnyName.i++; but to access "name" you must to create a new object. i hope u can fig out from here... do you remenber the "main" method this prints msg in a first empty class "hello world" you created? yep. it is simple as it is. i bet soon or latter u fig this out. take care
Joseph Haynes Level 2, United States of America, United States Expert
7 January 2020
Sadly, I am now completely confused about this "static" stuff..... But, I am sure it will eventually become clear.
Andrey Level 8, Newark, United States
30 August 2019
Pretty sure the code entry is bugged. I have rechecked my Code Entry nearly 10 times and then tried copy pasting from the exact pasted example, and it still marks that I didn't complete it. Not sure what is wrong, even added all the spaces and tabs exactly as the Repeat section lists it.
Tadeusz Sikora Level 13, Katowice, Poland
11 October 2019
yes, it seems to bug out on this line: System.out.println("Cat count at the second "
Johnson Jose Level 10, Edinburgh, United Kingdom
16 May 2019
If a class has the static modifier then: It makes your intention clear: this isn't just a class which happens to have all static members - it's a class which will only ever have static members.
Anastasia Falcon Level 8, Rio de Janeiro, Brazil
28 April 2019
I think those access modifiers (and the related stuff like what calls what) are the most difficult thing for me so far. Idk if it's just difficult because it's too much new info to process at once or aybe it's my weak point. Or the description is not clear enough =/
Mihai Bone Level 8, Bucharest, Romania
9 October 2020
Same for us, I do watch online explanation(when is not clear) and then come back here to train.
Anonymous #10428383 Level 12, Seattle, United States
14 April 2019
hmmm.. I think what you area saying here is not true: "The main purpose of using the static modifier in the class declaration is to control the relationship between the Cat and StaticClassExample classes. The idea is roughly this: the Cat class is not linked to StaticClassExample objects and access the instance (non-static) variables of the StaticClassExample class." 1 - "instance (non-static) variables" can not be accessed by static method /class, maybe you should say: The idea is roughly this: the Cat class is not linked to StaticClassExample objects and "CAN'T" access the instance (non-static) variables of the StaticClassExample class."
somecro Level 15, Zagreb, Croatia
14 April 2019
"the Cat class is not linked to StaticClassExample objects and "CAN'T" access the instance (non-static) variables of the StaticClassExample class" Exactly. This is what should be written in the text above. Also should have written that you can instantate static nested classes without referencing the outer class object.
Stas Level 11, Moscow, Russian Federation
18 January 2020
Static modifier need in class Cat, as it is used(new Cat) in Main method(Static!)