• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of static modifier

 
Stephen Silva
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the reply
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic