Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:53:51+00:00 2026-06-17T15:53:51+00:00

Possible Duplicate: When should a method be static? Usually when writing a static method

  • 0

Possible Duplicate:
When should a method be static?

Usually when writing a static method for a class, the method can be accessed using ClassName.methodName. What is the purpose of using ‘static’ in this simple example and why should/should not use it here? also does private static defeat the purpose of using static?

public class SimpleTest { 

   public static void main(String[] args) {
         System.out.println("Printing...");
         // Invoke the test1 method - no ClassName.methodName needed but works fine?
         test1(5);
   }

   public static void test1(int n1) {
         System.out.println("Number: " + n1.toString());
   }
   //versus
   public void test2(int n1) {
         System.out.println("Number: " + n1.toString());
   }
   //versus
   private static void test3(int n1) {
         System.out.println("Number: " + n1.toString());
   }
}

I had a look at a few tutorials. E.g. http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

My understanding of it is that instead of creating an instance of a class to use that method, you can just use the class name – saves memory in that certain situations there is no point in constructing an object every time to use a particular method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:53:52+00:00Added an answer on June 17, 2026 at 3:53 pm

    In this Example,Static is used to directly to access the methods.A private static method defeats the purpose of “Data hiding”.

    Your main can directly call test1 method as it is also Static,it dosn’t require any object to communicate.Main cannot refer non-static members,or any other non-static member cannot refer static member.

    "non-static members cannot be referred from a static context"

    You can refer This thread for more info about Static members.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: When should I use static methods in a class and what are
Possible Duplicate: Why does PHP 5.2+ disallow abstract static class methods? Why can't you
Possible Duplicate: PHP Using a variable when calling a static method I've read through
Possible Duplicate: Should Usings be inside or outside the namespace sa1200 All using directives
Possible Duplicate: Why should the interface for a Java class be prefered? When should
Possible Duplicate: Difference between static class and singleton pattern? Why would one ever require
Possible Duplicate: When should I use GET or POST method? What’s the difference between
Possible Duplicate: How should the Form.Load event be used compared to its constructor method?
Possible Duplicate: Consider providing static factory methods insteads of constructors This maybe a controversial
Possible Duplicate: ArgumentNullException or NullReferenceException from extension method? Which exception should be thrown here?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.