File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Use of static modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Use of static modifier" Watch "Use of static modifier" New topic

Use of static modifier

Stephen Silva

Joined: Jul 12, 2011
Posts: 18
Write an application where the static modifier becomes useful.

Can some one please, give me a good example to this assignment.

I have written a code to explain the consequences of static modifier.
But I need an application of that.
Prabhjot Jassal

Joined: Jul 19, 2010
Posts: 22
assume you are writing a soccer application :-)..and your task is to keep track of all the goals scored so far at an international level...static modifier is probably the best way to keep track of it
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
Static methods: Writing utility methods like finding the sum of two integers:

There is no need to create an instance of Utilities. All you want is calling the sum() method with two integers and getting the sum back.

Static variables: Counting the created instances of some class:

There is only one variable nMain per class. There is no point using an instance variable. What we want is one variable counting the created instances of the class Main.

Anyone an example for static nested classes?

Stephen Silva

Joined: Jul 12, 2011
Posts: 18
Thanks a lot for the answers.

But I need a simple application which shows the usage of the static modifier.
If you have any sample codes, but simple ones can you help me in this regard.

Anjali Vaidya
Ranch Hand

Joined: Jan 25, 2011
Posts: 40

Let us take an example when you want to use methods of another class but its constructor is private so that you cannot create an object. In that case, make the specific method which you want to use anyhow, as static

So basically, you can use static if you want to access any method or variables without creating object of the class.
You can also use static block

static blocks are executed before main(). So you can write statements here too. For example: giving some instructions to the user before he enters some value
Stephen Silva

Joined: Jul 12, 2011
Posts: 18
Thanks a lot for the reply
I agree. Here's the link:
subject: Use of static modifier
It's not a secret anymore!