Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By gimbal2

Thread: What is the use of Static method and static block

  1. #1
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default What is the use of Static method and static block

    what is the real use of static method and as well as static block?

    i know static methods are called without objects... without using objects what is the need of static methods?


    And the very important doubt is why static block?

    the static blocks are invoked before main method() invokes... So we can start the program or run the program without main.. so, if we have static block why we are using main()?

    please give me clarity on this static


    thank you in advance

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: What is the use of Static method and static block

    Static blocks are used to initialize static variables, not to start a program.

    Main is used to start an application because it allows arguments to be passed into the program.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: What is the use of Static method and static block

    Static methods are extremely useful for methods which do not require direct access to object state. I tend to think of them as helper or utility methods. Take a look at the java.lang.Math API. All of its methods are static (its constructor is declared private so you can't instantiate it).

    static blocks are used to initialize static variables. Especially if the variables are complex like a large array.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: What is the use of Static method and static block

    Quote Originally Posted by KevinWorkman View Post
    Main is used to start an application because it allows arguments to be passed into the program.
    we can also start an application using static blocks, can't we?

    you raise another doubt that is main method allows argument that is String args[].. okay.. what is the use of that argument? don't say that is for command line arguments. please give a better example instead of command line arguments?

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: What is the use of Static method and static block

    Quote Originally Posted by jim829 View Post
    Static methods are extremely useful for methods which do not require direct access to object state. I tend to think of them as helper or utility methods.
    Not only that, I use them to good effect when I have a design that I want to keep stateless. When you keep things static, you simply can't make the mistake of introducing state anyway without doing something that will shout WRONG! at you every time you read it.

    Be it web applications or games, there is always some part of the application framework that is simply better to keep stateless, be it to make it easier to scale the application or just for programming ease. And thus in that corner of the application there are lots of static methods. For the longest time I believed that when you introduce lots of statics your design is wrong and I think that for novices that is still true, but I know better nowadays ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: What is the use of Static method and static block

    Quote Originally Posted by chandrasekhar.melam View Post
    we can also start an application using static blocks, can't we?
    No.

    Quote Originally Posted by chandrasekhar.melam View Post
    you raise another doubt that is main method allows argument that is String args[].. okay.. what is the use of that argument? don't say that is for command line arguments. please give a better example instead of command line arguments?
    If you're confused, please ask a better question instead of "what is the use". It's used for command line arguments. If you're confused about something, I recommend doing a google search or asking a more specific technical question.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: What is the use of Static method and static block

    Static block are use to initialize static data.static blocks are called before main . You can execute a programe thorugh static block but not in jdk7 (in earlier versions).
    Go through this tutorial it will be helpful Static Blocks in Java
    Last edited by allaudin; 08-20-2013 at 03:52 PM.

  8. #8
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: What is the use of Static method and static block

    Quote Originally Posted by chandrasekhar.melam View Post
    we can also start an application using static blocks, can't we?

    you raise another doubt that is main method allows argument that is String args[].. okay.. what is the use of that argument? don't say that is for command line arguments. please give a better example instead of command line arguments?
    Command-line argumant are used when you want to give some information to your progame at start up. e.g Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment)

Similar Threads

  1. Replies: 3
    Last Post: 05-31-2012, 08:56 PM
  2. Replies: 9
    Last Post: 06-10-2011, 01:40 AM
  3. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  4. Replies: 3
    Last Post: 02-09-2010, 06:22 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •