Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By Norm

Thread: Unable to call classe constructor from main

  1. #1
    serdimay is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Unhappy Unable to call classe constructor from main

    I have created two classes SimpleList and SimpleListTest. The second is my main calling SimpleList. I am gettting a error while trying to do so . Here is my codes, any ideas?
    Java Code:
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class SimpleList   // default constructor
    {
    	private int num ;
    	private int smallest;
    	private int largest;
    	private int sum;
    	private int average;
    	private String response;
    	private char responseChar;
    	private int smallArray;
    	
    	
    	public int getData()  //public mutator to get the user inputs
    	{
    	return num;  // returns the input
    	}
    	
    	public void ProcessData () // calculates the sum of all values input (average,sum,largest and smallest number)
    	{
    	int [] smallArray = new int [10];
    	Scanner scanner = new Scanner(System.in);
    	do {
    		
    		System.out.println("Enter a floating number: ");
    		smallArray[num] = scanner.nextInt();
    		sum+= smallArray[num];
    		System.out.println("Do you want to continue? y or n?");
    		response = scanner.next();
    		responseChar = response.charAt(0);
    		num++;
    	} while (responseChar == 'y' && num < 10);
    	
    		average = sum/num;
    		}
    
    	public void display () // display message
    	{
    		int [] smallArray = new int [10];
    	
    	System.out.println("You have entered: ");
    	//System.out.println(Arrays.toString(smallArray));
    	System.out.println("\nYour sum is: " + sum);
    	System.out.println("\nYou average is: " + average);
    	
    	}
    	
    }
    	
    ********************************************************
    public class SimpleListTest
    {
    	public static void main (String [] args){
    	
    
    	Test test = new Test();
    	
    	SimpleList.display ();
    	
      
    	}
    }
    Last edited by sunde887; 08-29-2011 at 07:48 PM. Reason: Added code tags, [code]...[/code]

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Test test = new Test(); // what is Test.class ?

    SimpleList.display (); // display() isnt static, so first you have to create a SimpleList object!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I am gettting a error
    You forget to post the error messages.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    You forget to post the error messages.
    We're supposed to be psychic. Huh? You're not? How strange ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    serdimay is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Unhappy unable to call class constructor from main (next)

    The error is
    SimpleListTest.java:8 non static method display() cannot be referred from a static contex SimpleList.display();.

    I do not know how to call the method with object

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by serdimay View Post
    The error is
    SimpleListTest.java:8 non static method display() cannot be referred from a static contex SimpleList.display();.

    I do not know how to call the method with object
    You have two options:

    1) either you construct a SimpleList object and call the display() method on it;
    2) or you have to make the display() method static.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    The Dutch have more legal options for getting psychic than we do in the USA.
    Jodokus likes this.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    The Dutch have more legal options for getting psychic than we do in the USA.
    Yep, we tried everything; in the hippie age we did hashish, later we had our weed 'Nederwiet', recently we tried XTC and other pills; some of us even tried to smoke banana peels but I think we found walhalla now: politicians. We should grind and smoke the politicians for a better world.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    serdimay is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default unable to call classe constructor from main (updated)

    okay i have made the class display static, and made some changes in the main method. now when i call it it run the program and does not allow me to prompt for input. What am i missing. i am not getting any error, but the program does not run as wanted.
    here is what i have done from the previous

    import java.util.Arrays;
    import java.util.Scanner;

    public class SimpleList // default constructor
    {
    private int num ;
    private int smallest;
    private int largest;
    private static int sum;
    private static int average;
    private String response;
    private char responseChar;
    private int smallArray;


    public int getData() //public mutator to get the user inputs
    {
    return num; // returns the input
    }

    public void ProcessData () // calculates the sum of all values input (average,sum,largest and smallest number)
    {
    int [] smallArray = new int [10];
    Scanner scanner = new Scanner(System.in);
    do {

    System.out.println("Enter a floating number: ");
    smallArray[num] = scanner.nextInt();
    sum+= smallArray[num];
    System.out.println("Do you want to continue? y or n?");
    response = scanner.next();
    responseChar = response.charAt(0);
    num++;
    } while (responseChar == 'y' && num < 10);

    average = sum/num;
    }

    public static void display () // display message
    {
    int [] smallArray = new int [10];

    System.out.println("You have entered: ");
    System.out.println(Arrays.toString(smallArray));
    System.out.println("\nYour sum is: " + sum);
    System.out.println("\nYou average is: " + average);

    }

    }
    ***************************************
    this is the class method that calls it

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


    Test test = new Test();

    System.out.println("Enter a floating number: ");


    SimpleList.display ();


    }
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    When i call it it run the program and does not allow me to prompt for input.
    Where do you call any methods staring from the main() method that will prompt you for input?

    Where is the Test class?

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by serdimay View Post
    okay i have made the class display static
    This is almost always the wrong solution.

  12. #12
    sdevan03 is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default

    SimpleList obj = new SimpleList();
    obj.ProcessData();
    obj.display();

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    You forget to post the error messages.
    Quote Originally Posted by JosAH View Post
    We're supposed to be psychic. Huh? You're not? How strange ...

    kind regards,

    Jos ;-)
    And you didn't know that?

    db

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by DarrylBurke View Post
    And you didn't know that?
    It was a rhetorical question ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 08-30-2010, 08:08 PM
  2. Unable to call paint() from inside method
    By Y. Progammer in forum New To Java
    Replies: 3
    Last Post: 03-01-2010, 01:47 PM
  3. No main method, constructor/object not working
    By sinista in forum New To Java
    Replies: 9
    Last Post: 10-14-2009, 03:25 PM
  4. Constructor call not calling
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 06-09-2009, 02:06 AM
  5. Replies: 1
    Last Post: 07-20-2008, 05:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •