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 3 of 3

Thread: static variable in an instance method?

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default static variable in an instance method?

    public class Password {
     
        private static String[] passwords;
     
        public Password() {
     
            passwords = new String[] {"123", "ABC"};
        }
     
        public boolean isPasswordAuthentic(String password) {
     
            boolean isAuthentic = false;
     
            for (int x = 0; x <= passwords.length - 1; x++) {
     
                if (password.equals(passwords[x])) {
     
                    isAuthentic = true;
                    break;
                }
            }
     
            return isAuthentic;
        }
     
    }

    i have a static String array variable. why can i reference it in an instance method?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: static variable in an instance method?

    Can or can't?

    You should be able to. Static variables can be accessed from anywhere (following the rules of public, private, and protected of course), and anywhere includes inside of a non-static method.

    Think of static as another "object" that's created at run time that only contains static fields and static methods. It's referenced using the Class name.

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: static variable in an instance method?

    oh Im sorry to post this , I was confused of of my java codes.. anyway thanks for the response , Im sorry again..

Similar Threads

  1. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 08:09 AM
  2. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 04:36 AM
  3. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 01:56 PM
  4. Static method
    By kalees in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2009, 11:10 AM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM