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 / October 2005

Tip: Looking for answers? Try searching our database.

Help!  Constructors not working

Thread view: 
Luch - 27 Oct 2005 06:27 GMT
I'm doing an assignment for a Java class.  Basically, we have to build
constructors for a class, Television, whose attributes are  *brand* and
*price*. I have defined the constructors and accessor and mutator
methods in Television.java, and access it via TelevisionClient.java
(code for both is posted below).  Both files compile.  However when I
run TelevisionClient, it runs through the code, but my constructors,
tv0, which I assign a value in the program, and tv1, which is assigned
through dialog boxes at runtime, are the same value - in other words,
even assigning a value to one, they both have the values that are
passed in through the dialog box. I appreciate any help.

*******  Television.java  ****************

import java.util.*;
import javax.swing.JOptionPane;
import java.io.*;

public class Television
{
    public static String brand;
    public static String price;

    public Television(String xbrand, String xprice)
    {
        brand = xbrand;
        price = xprice;
    }

    public static String getBrand()
    {
        return brand;
    }

    public static String getPrice()
    {
        return price;
    }

    public String toString()
    {
        String str = "";
        str = "Television information:\n"
            + "The brand name is " + this.brand + "\n"
         + "The price is: $" + price;
        return str;
    }

    public boolean equals(Television tvx)
    {
        if (brand.equals(tvx.brand)
            && price==(tvx.price))
            return true;
        else
            return false;
    }
}
*******  end Television.java  ****************

*******  TelevisionClient.java  ****************

import javax.swing.JOptionPane;

public class TelevisionClient
{

    public static String inputMessage;
    public static String outputMessage;
    public static String outputMessage2;
    public static String outMsg;

 Television tv0, tv1;

 public void workWithTV( )
 {
   tv0 = new Television( "RCA", "100.00");

   JOptionPane.showMessageDialog( null, tv0.toString( ) );

        outputMessage = "This program will list the brand and price of a
television "
            + "based on the values you enter.\n"
            + "It will then compare the values to the default: RCA - $100.00\n";

        JOptionPane.showMessageDialog(null, outputMessage,"Television
Attributes",
            JOptionPane.PLAIN_MESSAGE);

        inputMessage = "Enter the brand of the television:\n\n";

        Television.brand = JOptionPane.showInputDialog(inputMessage);

        inputMessage = "Enter the price of the television:\n\n";

        Television.price = JOptionPane.showInputDialog(inputMessage);

//        price = Double.parseDouble(price);

        Television tv1 = new Television(Television.brand, Television.price);

        outputMessage = tv1.toString();
        JOptionPane.showMessageDialog(null, outputMessage,"Television
Attributes",
            JOptionPane.PLAIN_MESSAGE);

   JOptionPane.showMessageDialog( null, tv0.toString( ) );
   JOptionPane.showMessageDialog( null, tv1.toString( ) );

        outMsg = "tv0: " + tv0.brand + "/" + tv0.price + "\n"
            + "tv1: " + tv1.brand + "/" + tv1.price + "\n";

        JOptionPane.showMessageDialog(null, outMsg,"Television Attributes",
            JOptionPane.PLAIN_MESSAGE);

    if ( tv1.equals( tv0 ) )
       outputMessage2 = "DOES EQUAL";
    else
       outputMessage2 = "DOES NOT EQUAL";

        outputMessage = "The values you entered, brand: " +
Television.getBrand() + " / price: $" + Television.getPrice() + "\n"

   JOptionPane.showMessageDialog( null, outputMessage );

    if ( tv1 == tv0 )
       outputMessage2 = "DOES EQUAL";
    else
       outputMessage2 = "DOES NOT EQUAL";

        outputMessage = "The values you entered, brand: " +
Television.getBrand() + " / price: $" + Television.getPrice() + "\n"

   JOptionPane.showMessageDialog( null, outputMessage );
    }

    public static void main (String[] args)
    {
   TelevisionClient app = new TelevisionClient( );
   app.workWithTV( );
 }

}
*******  end TelevisionClient.java  ****************
Norb - 27 Oct 2005 06:34 GMT
Why are those fields "brand" and "price" in Television static?
Luch - 27 Oct 2005 06:57 GMT
Well, I admit I am not really clear on the static concept, but I when I
tried to compile, it told me "could not access from a non-static
reference", or something like that, on the calling line from
TelevisionClient.
Luch - 27 Oct 2005 06:58 GMT
Well, I admit I am not really clear on the static concept, but I when I
tried to compile, it told me "could not access from a non-static
reference", or something like that, on the calling line from
TelevisionClient.
Roedy Green - 27 Oct 2005 07:06 GMT
>public static String brand;
>    public static String price;
[quoted text clipped - 4 lines]
>        price = xprice;
>    }
Constructors rarely change static variables. You are constructing
facts about a particular television, not about televisions in general.

See http://mindprod.com/jgloss/static.html
http://mindprod.com/jgloss/instance.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

steen - 27 Oct 2005 10:35 GMT
Your problem is infact the static identifier like the others have
stated.

A static variable is shared between any instance of the class, so
infact even thou you have two different instances of Television, they
infact share the price and brand. So when u set the price and brand on
tv2, you also set those in tv1. Get rid of the static identifiers and
you should be ready to go.
steen - 27 Oct 2005 10:37 GMT
Oh and just read your code.

when you set the price and brand, you do so on the Television class,
thats why you get the error about them needing to be static.

What you're looking for is more along the lines of :

inputMessage = "Enter the brand of the television:\n\n";
String brand = JOptionPane.showInputDialog(inputMessage);
inputMessage = "Enter the price of the television:\n\n";
String price = JOptionPane.showInputDialog(inputMessage);
Television tv1 = new Television(brand, price);
Luch - 28 Oct 2005 06:48 GMT
Thanks, everybody, I appreciate the help.  The light is beginning to
dawn....


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



©2009 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.