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 10 of 10

Thread: instantiate problems

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default [SOLVED]instantiate problems

    hello i use netbeans and i was working on an example program for a class motorcycle
    which goes

    Motorcycle = new Motorcycle
    in this line the program returned an error that said Motorcycle cant be instantiated. pls what does dis mean
    Last edited by jamal; September 13th, 2010 at 06:35 PM. Reason: [SOLVED]


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: instantiate problems

    When you create a new Object, you need two things.
    1) a variable name
    2) a constructor

    So, if the Motorcycle constructor looked like this:
    public Motorcycle()
    {
    ...
    }

    You need to say:
    Motorcycle motor = new Motorcycle();
    In this, motor is our variable name and Motorcycle() is our call to Motorcycle's constructor that I mentioned above.

    Tell me if that helps.

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    jamal (September 13th, 2010)

  4. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: instantiate problems

    Please post the code for the Motorcycle class. There are a variety of reasons why the Motorcycle class can't be instantiated:

    1. The class is either an interface or declared abstract
    2. You're using the wrong syntax (as suggested by aussiemcgr)
    3. The class has no accessible constructors (for example a private or protected constructor)

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    jamal (September 13th, 2010)

  6. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: instantiate problems

    Really grateful 4 d assistance it really helped but i got anoda issue wich i dont quite undastand anytime i write a code like

    public static void main (String args[]) {

    i always get illegal start of expression, and also with something like dis

    void showAtts() {
    Last edited by jamal; September 13th, 2010 at 04:40 PM. Reason: spelling error

  7. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: instantiate problems

    If you're making a method you have to include private or public BEFORE the return.
    public void showAtts(){}
    //or
    private void showAtts(){}

    Here is how to set up a method:
    Modifier return-type name(parameter param) exceptions { //code }
    //example
    public String returnStringExample(String stringName) { //code }

    See this website for more information Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Last edited by Brt93yoda; September 13th, 2010 at 05:37 PM.

  8. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: instantiate problems

    @Brt93 i dont get you, am i expected to create a new method or can i just replace the method with something like dis

    public String returnStringAtts (String showAtts) {

    AM CONFUSED.just a beginner

    the web page aint opening,some kinda of error
    Last edited by jamal; September 13th, 2010 at 05:32 PM. Reason: incomplete

  9. #7
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: instantiate problems

    I don't know why the web page is freaking out. Anyways, all of it depends on what you are trying to do. If I were you I would go through some more tutorials. Here is a good place to start: http://download.oracle.com/javase/tutorial/

    I'm assuming what you're trying to do, so please correct me if I am going in the wrong direction.
    public void showAtts(int motorcycleNumber){
       if (motorcycleNumber == 1) { //do motorcycle 1 things }
       if (motorcycleNumber == 2) { //do motorcycle 2 things }
     //ETC...      
    }
    Last edited by Brt93yoda; September 13th, 2010 at 05:44 PM.

  10. #8
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: instantiate problems

    Thanks very much this program is from a text book am reading and it is part of the test programs but each time i write the entire program i dont make any head out of it, i always get error replies. for the showatts i already created the variables indicating the make and color of the motorcycle as follows

    public void showAtts() {
            System.out.println("This motorcycle is a "
                    + color +" " + make);
            if (engineState == true)
                System.out.println("The engine is on.");
            else System.out.println("The engine is off.");
     
        public static void main (String args[]) {
            Motorcycle motor = new Motorcycle();
            motor.make = "Yamaha RZ350";
            motor.color = "Yellow";
            System.out.println("Calling showAtts...");
            motor.showAtts();
            System.out.println("........");
            System.out.println("Starting engine...");
            motor.startEngine();
            System.out.println("........");
            System.out.println("Calling showAtts...");
            motor.showAtts();
            System.out.println("........");
            System.out.println("Starting engine...");
            motor.startEngine();
        }
        }

    now for each step i get illegal start of expression and in the case of the showAtts i get symbol not found

    i hope this would give u a beta idea of my problem. really appreciate the time
    Last edited by jamal; September 13th, 2010 at 05:54 PM. Reason: incomplete

  11. #9
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: instantiate problems

    This should work. I strongly suggest that you go through the tutorials in the link I posted earlier.

    public class motorcycle {
    	static String make,color;
    	static boolean engineState;
     
    	public static void startEngine(){
    		engineState = true;
    	}
     
    	public static void showAtts() {
            System.out.println("This motorcycle is a "
                    + color +" " + make);
            if (engineState == true)
                System.out.println("The engine is on.");
            else System.out.println("The engine is off.");
    	}
     
        public static void main (String args[]) {
            make = "Yamaha RZ350";
            color = "Yellow";
            System.out.println("Calling showAtts...");
            motorcycle.showAtts();
            System.out.println("........");
            System.out.println("Starting engine...");
            motorcycle.startEngine();
            System.out.println("........");
            System.out.println("Calling showAtts...");
            motorcycle.showAtts();
            System.out.println("........");
            System.out.println("Starting engine...");
            motorcycle.startEngine();
        }
    }
    Last edited by Brt93yoda; September 13th, 2010 at 06:06 PM.

  12. The Following User Says Thank You to Brt93yoda For This Useful Post:

    jamal (September 13th, 2010)

  13. #10
    Junior Member
    Join Date
    Sep 2010
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: instantiate problems

    REALLY GRATEFUL

Similar Threads

  1. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  2. Image problems
    By Bekuraryou1228 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 19th, 2010, 05:53 PM
  3. Exercise problems
    By Virgildonatifan in forum Java Theory & Questions
    Replies: 0
    Last Post: February 14th, 2010, 04:12 AM
  4. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM
  5. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM