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();"

Recommended Answers

All 2 Replies

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

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());
      }
}
commented: thank you thank you thank you +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.