| Author |
Use of static modifier
|
Stephen Silva
Greenhorn
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.
Thanks.
|
 |
Prabhjot Jassal
Greenhorn
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: 165
|
|
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?
John
|
 |
Stephen Silva
Greenhorn
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.
Thanks.
|
 |
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()
{
...
}
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
Greenhorn
Joined: Jul 12, 2011
Posts: 18
|
|
Thanks a lot for the reply
|
 |
 |
|
|
subject: Use of static modifier
|
|
|