Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

default value of data member.

Thread view: 
Mike - 29 May 2007 04:00 GMT
Hi

I have a problem about default value of data member!

+++++++ cut here++++++
class CComputer
{
  private double cpu=3.0;
  private double memory=1.0;

  public void set(double c)
  {
       if(c<0)
           System.out.println("Input cpu error, cpu is default value");
       else
       this.cpu=c;
  }
  public void set_memory(double m)
  {
       if(m<0)
           System.out.println("Input memory error, memory is default
value");
       else
       {
       memory=m;
           show_memory();
       }
  }

  public void set(double c,double m)
  {
   set(c);
   set_memory(m);
  }
  void show_cpu()
  {
    System.out.println("cpu="+this.cpu);
  }
  void show_memory()
  {
    System.out.println("memory="+this.memory);
  }
  void show_all()
  {
  this.show_cpu();
  this.show_memory();
  }
}
public class bbb
{
  public static void main(String args[])
  {
   CComputer c1=new CComputer();

   c1.set(-3.5,1.5);
   c1.show_all();
   c1.set(-3.5,-2.0);
   c1.show_all();
   c1.set(-3.5);
   c1.show_all();
  }
}
+++++++

The output of "c1.set(-3.5,-2.0);" shows memory=1.5.
The output of " c1.set(-3.5);" also shows memory=1.5;

Why?  I thought memory should be default value, i.e. 1.0.

Thank you in advance.

Mike
Daniel Pitts - 29 May 2007 05:11 GMT
> Hi
>
[quoted text clipped - 68 lines]
>
> Mike

You're default value only gets assigned during the construction of
your object.

your "new CComputer()" sets it to 1.0, You're first call to set(-3.5,
1.5) sets that value to 1.5, and it isn't ever reset.
Daniel Pitts - 29 May 2007 05:12 GMT
> Hi
>
[quoted text clipped - 68 lines]
>
> Mike

You're default value only gets assigned during the construction of
your object.

your "new CComputer()" sets it to 1.0, You're first call to set(-3.5,
1.5) sets that value to 1.5, and it isn't ever reset.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.