0

I'm very new in Java and I have a small question. I believe it is due to some misunderstanding of the concepts.

So, I have main class menu:

/**
 * menu.java
*/
public class menu {
    public void run() {
        println ("1. Option#1.");
        println ("2. Option#2.");
        println ("============");

        int choose = readInt("Enter a choice:");
        if (choose == 1) {
        // QUESTION>>>>>   // ### how can I call class option1.java here?

    }
}


/**
 * option1.java
*/
public class option1 { 
   public void scriepedos () { 
        setFont("Times New Roman-24");
        while (true) {
                String str = readLine("Please enter a string: "); 
                if (str.equals("")) break; 
                String rev = reverseString(str); 
                println(rev);
        }
    }

    private String reverseString(String str) {
        String result = "";
        for (int i=0; i<str.length();i++){
            result=str.charAt(i)+result;
        }
        return result.toLowerCase();
    }
}

Many thanks in advance. Leo

5
  • You can call the instance method of class by creating an object via Object reference. Commented Aug 16, 2012 at 8:21
  • You can call method of the class, you cann't call class Commented Aug 16, 2012 at 8:21
  • Thank you! I know it's a conceptual problem... Commented Aug 16, 2012 at 8:31
  • The question it is in code block. Commented Aug 16, 2012 at 9:24
  • but the new problem it is that nothing happened when I call the instance method. This is because "scriepedos" it's void ? Commented Aug 16, 2012 at 9:28

2 Answers 2

2

You need an instance of option1 to call upon e.g.

option1 o1 = new option1();
o1.scriepedos();

Alternatively you can make the method static. That means that you don't need the corresponding instance of the object e.g. in option1.java

public static void scriepedos () { ...

then in main.java

option1.scriepedos();

The above isn't very OO. You're now making use of the fact that you can have an object encapsulating state etc. and is a much more procedural style.

Notes:

  1. I suspect you need a public static void main() method to invoke the above
  2. Java style would require class names to be camel-cased. e.g. Option1, Main
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you all! About notes: 1. I realy have but I simplify the code :); 2. Thats are not my real class name :), but Thank you!
I do it but nothing it's happened when I call the instance option1.scriepedos(). This it's because scriepedos is "void" type ?
1

You can not call to class. You have to create new object of class and call it's methods like below :

Option1 op1 = new Option1();
// call any Option1 method
op1.scriepedos();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.