CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Why will this block not complie? (http://www.codingforums.com/showthread.php?t=282175)

abell12 11-14-2012 04:10 PM

Why will this block not complie?
 
Code:

    public void memberRegister(String newName, String newId, int newPinNumber)
    {
        String name = newName;
        String id = newId;
        int pinNumber = newPinNumber;
       
        Member.welcomeMessage(String name, String id, String storeName);
    }


alykins 11-14-2012 04:37 PM

String 'storeName' is not declared; presumably Member is outside this block... need more code and error message to give an accurate answer though

abell12 11-14-2012 04:40 PM

Ok sorry didnt provide enough info.

storeName is a field in the class, so thats valid.

Error on this line:
Member.welcomeMessage(String name, String id, String storeName);

Says this:
')' Expected

alykins 11-14-2012 05:51 PM

I'm not seeing it... can you post entire class?

Fou-Lu 11-14-2012 06:24 PM

You can't call a method like that. It expects those argument datatypes, you cannot respecify them in the method call itself. Give it name, id and storename, not String name, ... instead.

alykins 11-14-2012 06:36 PM

I can't believe I didn't catch that :| it looked so correct for a constructor the fact the method call was bad eluded me *smack

abell12 11-14-2012 06:46 PM

Can you fix it for me.
Here is the store class:

Code:

public class Store
{
    // instance variables
    private String storeName;
    private int total;

    /**
    * Constructor for objects of class Store
    */
    public Store(String newStoreName)
    {
        storeName = newStoreName;
        total = 0;
    }
   
    /**
    * Register a member
    */
    public void memberRegister(String newName, String newId, int newPinNumber)
    {
        String name = newName;
        String id = newId;
        int pinNumber = newPinNumber;
       
        Member.welcomeMessage(String name, String id, String storeName);
    }
}

And here is the welcomeMessage method which is in the member class:
Code:

    /**
    * Welcome Message
    */
    public void welcomeMessage(String newName, String newId, String newStoreName)
    {
        System.out.println(newStoreName + ": Welcome " + newName + " (id: " + newId + ")");
    }


Fou-Lu 11-14-2012 07:03 PM

Yeah, use this: Member.welcomeMessage(name, id, storeName);.

abell12 11-14-2012 07:28 PM

Quote:

Originally Posted by Fou-Lu (Post 1291962)
Yeah, use this: Member.welcomeMessage(name, id, storeName);.

Using that line i now get this error:

non-static method welcomMessage(java.lang.String,java.lang.String,java.lang.String) cannot be referenced from a static context

Fou-Lu 11-14-2012 07:51 PM

You need to provide it with an object then. WelcomeMessage is a non-static method, so it must be called on an instance of an object.

abell12 11-14-2012 08:27 PM

How do I do that?

Fou-Lu 11-14-2012 10:07 PM

The only direct way to do it with the code you have here is to instantiate a new member within the method memberRegister. I would assume that memberRegister should be doing something with Member anyway, so that may be a fine place to construct a new member.

vision_slr 11-15-2012 04:10 AM

Instantiating a Member object would depend on the constructors available.

Here's an example of instantiating a Member if it has a default constructor (no arguments)

Code:

public void memberRegister(String newName, String newId, int newPinNumber)
    {
       
        String name = newName;
        String id = newId;
        int pinNumber = newPinNumber;
        Member member = new Member();
        member.welcomeMessage(name, id, storeName);
    }

Here's the Java trail on constructors for your reference:
http://docs.oracle.com/javase/tutori...structors.html

abell12 11-15-2012 03:24 PM

Thanks for all your help. I have come across another problem if you dont mind helping me out.
Heres the link:
http://www.codingforums.com/showthread.php?t=282258


All times are GMT +1. The time now is 10:22 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.