0

Hello. Im new to Java. I don't know much about Java's codes but I have a good background in C. Im using NetBeans 6.7.
I have this code:

package javaapplication1;


public class Main {
    public int x = 3;
    
    public int test(){
        x += 4;
        return x;
    }
    public static void main(String[] args) {
     
        test x = new test();
        System.out.print("Hello World" + x.test);
     
    }

}

I want to do an operation of integer x from another object then print it in Main. I can't get the code to run. The compiler is underlining the two "test" in statement "test x = new test();"

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by __avd
-1

the code you currently have is trying to construct a test object, but test is simply a method you call. therefore, you can simply say

x = test();

, furthermore when you are printing the result, is it simply

x
1

silliboy, a silly mistake.
Data member x and method test are instance memebers of class.

package javaapplication1;
public class Main {
    public int x = 3;  // Instance field
    public int test()  // Instance method
   {
        x += 4;
        return x;
    }
    public static void main(String[] args) {
          //Create an instance of class Main
          Main p=new Main();   
          System.out.print("Hello World" + p.test());
      }
}
Votes + Comments
thank you thank you thank you
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.