Ok so when I run the following code nothing occurs so could someone look at the code and tell me where I have gone wrong?

public class somee  { 
    public static void main(String[] args) { 





       somee f = new somee();


}


public void f() {
           int anInt = g(7);
           System.out.println("ffff"+anInt);
       }

   public int g(int arg) {
           if(arg < 7) {
               return arg + 2;
       }
        else if( arg > 6) {
            return arg + 3;
        }
        else {
            return arg + 4;
        }
    }



}

Your main method runs, it creates an instance of your class, and then it's finished.
If you add some code to call the "f" method then you should see some output.

ps having a variable called f and a method called f isn't an error, but it is potentially confusing.

Edited 4 Years Ago by JamesCherrill

Ok thanks but this is what I am trying to do is print the results from 'public void f()'. So what would I need to change? As within my 'public void f()' is dosn't even produce ' System.out.println("ffff");' when I call it.

Edited 4 Years Ago by TIM_M_91

public void f() defines a method, but it doesn't execute it. You must call the method to get it executed. Do you know how to call a method?

Thats what I'm trying to work out so far I have done this:

public class somee  { 
    public static void main(String[] args) { 





       somee f = new somee();

 int g = f(); 

 System.out.println(g);


}

My 'System.out.println(g);' will print my int g but I'm having trouble with called my method to 'int g' so if you could help it would be appriciated.

So now you're really getting confused by having variables and methods with the same names. Please don't do that!

You already have code to call your g method. It's this line:
int anInt = g(7);
That line executes the code in method g and returns the value that g calculates. That result is then assigned to the variable anInt.

Ok to make it easier to understand I have done this:

public class somee  { 
    public static void main(String[] args) { 
       somee f = new somee();
 System.out.println(f);      
}
public void f() {
          System.out.println("gggg");
       }
}

So what would need to be done to get 'System.out.println("gggg");' to print as when I run the program I get 'somee@19821f' ?

One last time. Change the names of the variables or methods so they don't have the same name - that is confusing you.
The variable "f" that you print on line 4 has absolutely nothing to do with the method "f" that you define on line 6. If you simply change the names then that is a lot more obvious:

somee anInstanceOfSomee = new somee();
System.out.println(anInstanceOfSomee);

public void myPrintingMethod() {
   System.out.println("gggg");
}

Ok thanks for your help fixed it:

public class somee  { 
    public static void main(String[] args) { 
 f();   
}
public static void f() {
          int anInt = g(7);
           System.out.println(anInt);
       }

public static int g(int arg) {
           if(arg < 7) {
               return arg + 2;
       }
        else if( arg > 6) {
            return arg + 3;
        }
        else {
            return arg + 4;
        }
    }
}

Yeah, that's good!
Just FYI now, you didn't need to change the methods to static, and over-using statics is leading you in a bad direction for your future learning about object-oriented code. You can leave them as instance methods and call them using the instance, eg

 public static void main(String[] args) {
     somee anInstance = new somee();
     anInstance.f();
This article has been dead for over six months. Start a new discussion instead.