.class vs. .java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ITQUEST
    New Member
    • Feb 2007
    • 11

    .class vs. .java

    The moderator posted this in another thread:
    (I am starting a new one not to confuse the other subject at hand)

    ".java classes are compiled to into .class files. If you have a .java file called Product.java, when you compile it a .class is created for every class in the file Product.java if there are no errors. In this case you are getting this error because there is no class file called Product.class so you must compile the Product class first."

    I don't really understand how to do this. When I go to the Command Prompt and type in the instructions given to create the .java file it creates a .class file, but then when I try to run it, it doesn't work and I get the same error.

    This is the info I have and am not sure if it all goes in the Java file or in two separate files:
    //Product1 class

    public class Product1 {

    private String title;
    private double item;
    private double stock;
    private double price;

    //Constructor to initialize product information
    public Product(String title, double item, double stock, double price) {

    setTitle(title) ;
    setItem(item);
    setStock(stock) ;
    setPrice(price) ;

    }

    // Method to get title
    public String getTitle() {
    return title;
    }

    // Method to get item number
    public double getItem() {
    return item;
    }

    // Method to get stock
    public double getStock() {
    return stock;
    }

    // Method to get price
    public double getPrice() {
    return price;


    }

    // Method to set title name
    public void setTitle(String title) {
    this.title = title;
    }

    // Method to set item number
    public void setItem(double item) {
    this.item = item;
    }

    // Method to set stock available
    public void setStock(double stock) {
    this.stock = stock;
    }

    // Method to set price
    public void setPrice(double price) {
    this.price = price;
    }

    // Method to compute value of inventory
    public double getInvValue() {
    return this.stock *this.price;
    }

    }//end class Product1

    //Inventory program

    public class Inventory
    {
    public static void main (String args [])
    {

    System.out.prin tln ("Inventory of DVD Movies:\n");

    String title = "Notebook";
    double item = 1;
    double stock = 27;
    double price = 19.00;
    double value;

    Product product = new Product (title, item, stock, price);

    System.out.prin tf("%s%18s\n", "DVD Title:", product.getTitl e() );

    System.out.prin tf("%s%16s\n", "Item #:", product.getItem () );

    System.out.prin tf("%s%8s\n", "Number in Stock:", product.getStoc k() );

    System.out.prin tf("%s%18s\n", "Price:", product.getPric e() );



    System.out.prin tf("%s%9s\n", "Inventory Value:", product.getInvV alue() );

    }

    }//end class Inventory


    Thanks! ITQUEST
  • hirak1984
    Contributor
    • Jan 2007
    • 316

    #2
    How much I remember you got a noclassdeffound exception right?

    Let me tell you that it appears when you use methods from a class which is not in the same package.
    either you have to keep them in the same package, or give the fully qualified path of the class.
    In this case it seems that your product1 class is not converted into class file,that is why main could not find the mothods you have called from main.

    Do one thing compile Product1 class separately(keep ing in a separate .java file).Hope it will work.
    Good luck.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by ITQUEST
      The moderator posted this in another thread:
      (I am starting a new one not to confuse the other subject at hand)

      ".java classes are compiled to into .class files. If you have a .java file called Product.java, when you compile it a .class is created for every class in the file Product.java if there are no errors. In this case you are getting this error because there is no class file called Product.class so you must compile the Product class first."

      I don't really understand how to do this. When I go to the Command Prompt and type in the instructions given to create the .java file it creates a .class file, but then when I try to run it, it doesn't work and I get the same error.

      This is the info I have and am not sure if it all goes in the Java file or in two separate files:
      //Product1 class

      public class Product1 {

      private String title;
      private double item;
      private double stock;
      private double price;

      //Constructor to initialize product information
      public Product(String title, double item, double stock, double price) {

      setTitle(title) ;
      setItem(item);
      setStock(stock) ;
      setPrice(price) ;

      }

      // Method to get title
      public String getTitle() {
      return title;
      }

      // Method to get item number
      public double getItem() {
      return item;
      }

      // Method to get stock
      public double getStock() {
      return stock;
      }

      // Method to get price
      public double getPrice() {
      return price;


      }

      // Method to set title name
      public void setTitle(String title) {
      this.title = title;
      }

      // Method to set item number
      public void setItem(double item) {
      this.item = item;
      }

      // Method to set stock available
      public void setStock(double stock) {
      this.stock = stock;
      }

      // Method to set price
      public void setPrice(double price) {
      this.price = price;
      }

      // Method to compute value of inventory
      public double getInvValue() {
      return this.stock *this.price;
      }

      }//end class Product1

      //Inventory program

      public class Inventory
      {
      public static void main (String args [])
      {

      System.out.prin tln ("Inventory of DVD Movies:\n");

      String title = "Notebook";
      double item = 1;
      double stock = 27;
      double price = 19.00;
      double value;

      Product product = new Product (title, item, stock, price);

      System.out.prin tf("%s%18s\n", "DVD Title:", product.getTitl e() );

      System.out.prin tf("%s%16s\n", "Item #:", product.getItem () );

      System.out.prin tf("%s%8s\n", "Number in Stock:", product.getStoc k() );

      System.out.prin tf("%s%18s\n", "Price:", product.getPric e() );



      System.out.prin tf("%s%9s\n", "Inventory Value:", product.getInvV alue() );

      }

      }//end class Inventory


      Thanks! ITQUEST
      These classes are both public classes. So each one must go into its own .java file. i.e You can only have one public class per .java file and the name of that .java file must be the same as the name of that public class. When you go to the command prompt, you do not create .java files there. You create .java files by simply saving a file with the .java extension. You can type a program in notepad or textpad and save it as a .java file, thus creating the .java. When you go to the command and type javac FileName.java, you are now compiling the .java file. If the file is a valid java source file (no errors) then you get class files created otherwise errors will be shown on the prompt and no files are created. You can read a bit more about this in the Java classes on this page.

      Comment

      • ITQUEST
        New Member
        • Feb 2007
        • 11

        #4
        Thanks to both of you for the explanations. They made sense to me and I was able to correctly complete my assignment because of this.


        Thanks Again!
        ITQUEST

        Comment

        Working...