Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to return class instance?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to return class instance?

    I have the next code:

    import java.sql.*;
     
    public class Products {
    private int ProductID;
    private String name;
    private String category;
    private Double price;
    public void setId(int ProductID) { this. ProductID = ProductID; }
    public int getId() { return this.ProductID; }
    public void setName(String name) { this.name = name; }
    public String getName() { return this.name; }
    public void setCategory(String category) { this. category = category; }
    public String getcategory () { return this.category; }
    public void setPrice(double price) { this.price = price; }
    public double getCijena() { return this.price; }
    public Products(int ProductID, String name, String category, Double price)
    {
        this.ProductID = ProductID;
        this.name = name;
        this.category = category;
        this.price = price;
      }
    public static void getFromId(int productID)
    {
    Connection conn = null;
       {
    try
        {
          Class.forName ("com.mysql.jdbc.Driver").newInstance ();
              conn = DriverManager.getConnection ("jdbc:mysql://localhost/prodaja", "root", "");
            System.out.println(conn.isClosed());
        }
    catch(Exception ex)
    { 
    System.out.println("Connection not established"); 
    }
     try
    {
     Statement st = conn.createStatement();
        st.executeQuery("select * from products WHERE ID_product="+productID);
        ResultSet rs = st.getResultSet();
        while (rs.next()) 
        {
            int IdP = rs.getInt(1);
            String Pname = rs.getString(2);
            String Pcategory = rs.getString(3);
            Double Pprice = rs.getDouble(4);
            Products p=new Products(IdP, Pname, Pcategory, Pprice);
        }
    }
       catch (SQLException s){
      System.out.println("SQL statement not executed!");
    }
     
    finally {
    try
        {
        if(conn!=null && !conn.isClosed())
            conn.close();
        System.out.println(conn.isClosed());
        }
    catch(Exception ex) { }
     
     }
    }

    Now, what I need is to make this method(getFromId) return this newly created class instance, with its fields values.
    Any suggestons?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to return class instance?

    I'm assuming you want to return your Products object, so all you need to do is change the method declaration from void, to a return type of Products.
    then in your method, use return p;

    E.g
    To write a method which returns a String, you would do:
    public String getString(){
       String x = "Hello World";
       return x;
    }
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to return class instance?

    Quote Originally Posted by newbie View Post
    I'm assuming you want to return your Products object, so all you need to do is change the method declaration from void, to a return type of Products.
    then in your method, use return p;

    E.g
    To write a method which returns a String, you would do:
    public String getString(){
       String x = "Hello World";
       return x;
    }
    I tried that, but it's not working.
    I get error message saying:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
    at sales.Product.getFromId(Product.java:158)
    at sales.Sales.main(Sales.java:18)
    Java Result: 1

    "Sales" is the name of main class, from which I call method(getFromID)

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to return class instance?

    Well the answer is in the error message, " missing return statement".
    Take the String example, if you declare a method such as it returns a String, you really do need to return a String.

    If you're still having trouble, be sure to post the code snippet to which your error message is referring to.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to return class instance?

    Quote Originally Posted by newbie View Post
    Well the answer is in the error message, " missing return statement".
    Take the String example, if you declare a method such as it returns a String, you really do need to return a String.

    If you're still having trouble, be sure to post the code snippet to which your error message is referring to.
    Here is the code snippet (to which error message is referring to):
    class Product
    public static Product getFromId(int productID)
    {//this is the line 158//
    main class(Sales)
    Product.getFromId(1001);
    Last edited by nera1981; August 23rd, 2011 at 12:09 PM.

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to return class instance?

    Your class is Products not Product
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to return class instance?

    Quote Originally Posted by newbie View Post
    Your class is Products not Product
    It was, but I changed it to Product, because it should contain data for one product.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: How to return class instance?

    Cross posted at classInstanceInitalization - CodeGuru Forums

Similar Threads

  1. Diff between spring created been and instance of class
    By tcstcs in forum Web Frameworks
    Replies: 0
    Last Post: April 5th, 2011, 12:54 AM
  2. [SOLVED] Return randomized String to other class in project
    By Fermen in forum Collections and Generics
    Replies: 2
    Last Post: February 16th, 2011, 04:36 PM
  3. Replies: 2
    Last Post: January 22nd, 2011, 10:20 PM
  4. Compare instance of a class to another
    By srs in forum Java Theory & Questions
    Replies: 5
    Last Post: December 2nd, 2010, 09:39 AM
  5. Replies: 0
    Last Post: February 2nd, 2010, 07:20 AM