Doing operations from different object help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 61
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Doing operations from different object help

 
0
  #1
Jul 23rd, 2009
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:

  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();"
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Doing operations from different object help

 
0
  #2
Jul 23rd, 2009
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
  1. x = test();
, furthermore when you are printing the result, is it simply
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,712
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 493
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Doing operations from different object help

 
1
  #3
Jul 23rd, 2009
silliboy, a silly mistake.
Data member x and method test are instance memebers of class.
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC