943,694 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 452
  • Java RSS
Jul 23rd, 2009
0

Doing operations from different object help

Expand Post »
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:

java Syntax (Toggle Plain Text)
  1. package javaapplication1;
  2.  
  3.  
  4. public class Main {
  5. public int x = 3;
  6.  
  7. public int test(){
  8. x += 4;
  9. return x;
  10. }
  11. public static void main(String[] args) {
  12.  
  13. test x = new test();
  14. System.out.print("Hello World" + x.test);
  15.  
  16. }
  17.  
  18. }
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();"
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008
Jul 23rd, 2009
-1

Re: Doing operations from different object help

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
java Syntax (Toggle Plain Text)
  1. x = test();
, furthermore when you are printing the result, is it simply
java Syntax (Toggle Plain Text)
  1. x
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Jul 23rd, 2009
1

Re: Doing operations from different object help

silliboy, a silly mistake.
Data member x and method test are instance memebers of class.
java Syntax (Toggle Plain Text)
  1. package javaapplication1;
  2. public class Main {
  3. public int x = 3; // Instance field
  4. public int test() // Instance method
  5. {
  6. x += 4;
  7. return x;
  8. }
  9. public static void main(String[] args) {
  10. //Create an instance of class Main
  11. Main p=new Main();
  12. System.out.print("Hello World" + p.test());
  13. }
  14. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java date validation
Next Thread in Java Forum Timeline: pricing example project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC