Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Static Methods Preventing Usage of Instances?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Static Methods Preventing Usage of Instances?

    Hello,

    Copeg's post towards this thread:
    This fully depends upon how one wishes to use the method (unclear - at least to me - at this point) Declaring the method as static precludes one from using any sort of object oriented programming, thus the method cannot access instance fields of the object if it needs to.
    I created two short classes to sort of find out what this meant, but I feel I do not understand it well enough.

    Test class (main):
    package votek;
     
    public class Test {
     
    	public static void main(String[] args) {
    		SomeMethod();
    	}
     
    	public static void SomeMethod() {
    		Character character = new Character();
    		character.totalLevel = 50;
     
    		System.out.println(character.totalLevel);
    	}
     
    }

    Character class:
    package votek;
     
    public class Character {
    	private int level = 0;
     
    	public Character() {
    		level = 50;
    	}
     
    }

    OR

    Does it mean that making a static method in the class with private instances will prevent the method from using the private instances?
    Last edited by Votek; June 18th, 2014 at 12:55 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Static Methods Preventing Usage of Instances?

    Your example would be more representative of the use of OOP and related to Copeg's comment, if it were constructed like this:
    public class TestClass
    {
     
        public static void main(String[] args)
        {
            Character character = new Character();
            character.someMethod();
        }
     
    } // end class TestClass
    public class Character
    {
        private int level = 0;
     
        public Character()
        {
            level = 50;
        }
     
        public void someMethod()
        {
            System.out.println( "Level = " + level );
        }
     
    } // end class Character

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Votek (June 17th, 2014)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Static Methods Preventing Usage of Instances?

    And to extend off of Greg's code and the situation as I understood it in the linked post 1) suppose another class wishes to call 'someMethod', but you don't have a reference to a Character object (or don't know how to). 2) You choose to make someMethod static so you can just access it by calling Character.someMethod(). 3) A compile time error will result because 'level' is not static. 4) You can choose to make level static, but now its value will be the same for each object that is an instance of Character. 5) An alternative when given the situation in (1), is to somehow pass the reference of the object to the other class so that it can call the method.

  5. The Following User Says Thank You to copeg For This Useful Post:

    Votek (June 17th, 2014)

  6. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Static Methods Preventing Usage of Instances?

    Tested the error and understand what you meant now.

    5) An alternative when given the situation in (1), is to somehow pass the reference of the object to the other class so that it can call the method.
    Is this the way that is supposed to be done?
    (Apologize if many questions follow.)

    TestClass class (main):
    package votek;
     
    public class TestClass {
     
    	public static void main(String[] args) {
    		Character knight = new Character(78);
    		Character mage = new Character(53);
    		Character ranger = new Character(29);
     
    		Team team = new Team();
     
    		team.sayCurrentLevel(knight);
     
    	}
     
    } //end class TestClass

    Character class:
    package votek;
     
    public class Character {
     
    	private int level = 0;
     
    	public Character(int level) {
    		this.level = level;
    	}
     
    	public void someMethod() {
    		System.out.println("Level = " + level);
    	}
     
    } //end Character class

    Team class:
    package votek;
     
    public class Team {
     
    	public void sayCurrentLevel(Character character) {
    		character.someMethod();
    	}
     
    } //end Team class

  7. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Static Methods Preventing Usage of Instances?

    Tested the error and understand what you meant now.
    Great!

    Is this the way that is supposed to be done?
    Yes...if that's the behavior you want. Sorry for the vague answer, but answers to questions like this are very often context based (the context being what one wishes the program to do). While there are best practices there often is not really a 'supposed to' unless the code doesn't compile, throws Exceptions at runtime, or misbehaves.

  8. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Static Methods Preventing Usage of Instances?

    Quote Originally Posted by copeg View Post
    Great!



    Yes...if that's the behavior you want. Sorry for the vague answer, but answers to questions like this are very often context based (the context being what one wishes the program to do). While there are best practices there often is not really a 'supposed to' unless the code doesn't compile, throws Exceptions at runtime, or misbehaves.
    Thank you for your time!

Similar Threads

  1. instantiating static methods
    By nickdesigner in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 23rd, 2013, 08:47 PM
  2. Static methods in java
    By paramjit.singh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2013, 07:11 AM
  3. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  4. When to use static methods?
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: June 7th, 2012, 01:51 AM
  5. Static Methods Problem
    By mysticCHEEZE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 09:09 AM