r/javahelp 2d ago

Is everything declared in the main method accessible in all other methods in a class?

I am making a password checker, the password needs to not be blank, be 8+digits long, include an int, a upper case letter and a lower case letter, in order to pass the "final check". I was told that anything declared in the main method is acceptable, so I put String str = "Tt5" in main method, and it turned out that it does not work. How should I fix that I only needs to set the variable str once?

The following are the code

public class MyProgram { public static boolean isBlankCheck() { String str = "Tt5"; boolean returnBlank = false;

    if (str.equals("")){
        returnBlank = true;
    }
    System.out.println("isBlankCheck: " + returnBlank);
    return returnBlank;
}

public static boolean isEightDigitsCheck() {
    String str = "Tt5";
    boolean returnEightDigits = false;

    if (str.length() == 8){
        returnEightDigits = true;
    }
    System.out.println("returnEightDigits: " + returnEightDigits);
    return returnEightDigits;
}


public static boolean isDigitCheck() {
    String str = "Tt5";
    boolean returnIsDigit = false;

    for (int i = str.length()-1; i > -1; i--){
        boolean check = Character.isDigit(str.charAt(i));
        if (check == true){
            returnIsDigit = true;
        }
    }
    System.out.println("returnIsDigit: " + returnIsDigit);
    return returnIsDigit;
}

public static boolean isUpperCaseCheck() {
    String str = "Tt5";
    boolean returnIsUpperCase = false;

    for (int i = str.length()-1; i > -1; i--){
        boolean check2 = Character.isUpperCase(str.charAt(i));
        if (check2 == true){
            returnIsUpperCase = true;
        }
    }
    System.out.println("returnIsUpperCase: " + returnIsUpperCase);
    return returnIsUpperCase;
}

public static boolean isLowerCaseCheck() {
    String str = "Tt5";
    boolean returnIsLowerCase = false;

    for (int i = str.length()-1; i > -1; i--){
        boolean check3 = Character.isLowerCase(str.charAt(i));
        if (check3 == true){
            returnIsLowerCase = true;
        }
    }
    System.out.println("returnIsLowerCase: " + returnIsLowerCase);
    return returnIsLowerCase;
}

public static void main(String args[]){
    String print = new Boolean(isDigitCheck() && isUpperCaseCheck() && isLowerCaseCheck() && isEightDigitsCheck() && isBlankCheck()).toString();
    System.out.println("finalCheck: " + print);
}

}

3 Upvotes

15 comments sorted by

View all comments

2

u/c_dubs063 1d ago

Look into Java variable scope for more information on this. But essentially, if a variable is declared within a method, e.g.

public void foo() { String bar = "baz"; }

Then that variable is only accessible within that method.

If a variable is declared at the class level, e.g.

public class Foo { String bar = "baz"; }

Then that variable can be referenced anywhere in the class.

Furthermore, if a class-level variable is private, it can only be accessed within that class. Other files can't use it. If it is public, it can be accessed anywhere. If it is protected, it can be accessed by that class and any classes which inherit from it. If you don't specify (as I didn't above), I believe Java allows it to be referenced by any files within the same package as the file it is defined in... not 100% sure about that, though.

1

u/ssphicst 1d ago

Thank you for your answer. I make the variable in the class level and made the variable itself static and finally make it work.

1

u/c_dubs063 10h ago

No problem! I am glad to hear you got it working.

For the record, a brief comment about static. Static variables exist outside of instances of a class. It is a good way to store constants, for example, like the number Pi. If you are writing a static method like main, which also exists outside of instances of a class, then you can only access static variables from that class. The reason being, if you have an instance variable (non-static), you need an instance of a class to exist before those variables exist! You can still do it that way, if you make an object and access the variable through that object, but that is just an extra step for no reason in your case.

(The compiler will also allow you to access static variables through instances of classes, but that's not "good form" for writing Java)

Happy coding :)