| | |
Doing operations from different object help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
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();"
I have this code:
java Syntax (Toggle Plain Text)
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); } }
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 , furthermore when you are printing the result, is it simply
java Syntax (Toggle Plain Text)
x = test();
java Syntax (Toggle Plain Text)
x
silliboy, a silly mistake.
Data member x and method test are instance memebers of class.
Data member x and method test are instance memebers of class.
java Syntax (Toggle Plain Text)
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()); } }
![]() |
Similar Threads
- how to create the dynamic object using C# (C#)
- is conversion from void pointer to class object pointer valid?? (C++)
- Writing a Program using "pass-by-reference" functions. (C++)
- Shared Memory Object Access (C++)
- object reference not set to instance of object+axis2 (Java)
- Python class object - another strange behaviour (Python)
- Please help me to convert a simple C++ program into an object-oriented one. (C++)
- different minOccurs for different operations? (XML, XSLT and XPATH)
- [B]problem accessing object(s) in vector [/B] (Java)
- Object Oriented Design (PHP)
Other Threads in the Java Forum
- Previous Thread: java date validation
- Next Thread: pricing example project
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes clear client code component consumer database draw eclipse ee error event exception fractal free game gameprogramming gis givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javaprojects jni jpanel julia jvm key linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows






