• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use of static modifier

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 40
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the reply
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic