simple error

The following code throws an error, the error is just below the code, what is wrong?,

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.applet.*;


class Sender extends Applet (String str){}


--------------------Configuration: j2sdk1.4.0_02 <Default>--------------------
C:\r.c.p\Sender.java:10: '{' expected
class Sender extends Applet (String str){}
                            ^
C:\r.c.p\Sender.java:10: '}' expected
class Sender extends Applet (String str){}
                                          ^

Process completed.

irish_paddyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
public class Sender extends Applet
{
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
If you wanted a constructor in there too:

public class Sender extends Applet
{
   public Sender(String str)
   {
      this.str = str;
   }
   
   private String str;
}

0
objectsCommented:
Passing a parameter to an Applet is pointless and that code will not even load.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

CEHJCommented:
Depends on how the applet is going to be used
0
objectsCommented:
there is only one way :-D
0
CEHJCommented:
This would be a perfectly legitimate way:

Sender sender = new Sender("MySender");
add(sender);
setTitle(sender.getStr());
0
objectsCommented:
But pointless, and really not worth commenting. It only confuses people that come here to learn.
0
CEHJCommented:
I'm not sure how you can make assumptions about how irish_paddy is going to use this Applet...

Just as a precaution though, irish_paddy: if you were running the applet in a browser or the appletviewer then the empty constructor will be used as opposed to one with arguments.
0
objectsCommented:
The code you post won't load in a browser or appletviewer (Which is what applet are for after all, no big assumption there) so I don't see how it is a useful comment to make.
0
CEHJCommented:
You're making assumptions i'm not making. irish_paddy seems to have being thinking about a String argument constructor and there's no reason why he can't have one.
0
objectsCommented:
Except that it'll never get called in standard applet use, and it will stop the applet from loading at all. Great suggestion :-D

irish_paddy,

If you want to pass a string to your applet then it is done applet applet params in your html. And the code would be:

public class Sender extends Applet
{
   private String S = null;

   public void init()
   {
      S = getParameter('S");
   }
}

http://java.sun.com/docs/books/tutorial/applet/appletsonly/getParam.html
http://java.sun.com/docs/books/tutorial/applet/index.html
0
irish_paddyAuthor Commented:
I want to pass a string to a new class like so, I cant see why the followiing code wont work...

     Sender s = new Sender("sd");

//a seperate file
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.applet.*;


public class Sender(String sender)
{
   
}
0
girionisCommented:
 Because it is not syntactically correct. You can only pass parameters to the contructor or, in the case of applet, throught the initialization parameters in the HTML file (you can also pass them in the constructor as CEHJ suggested but it is practically pointless).

  You have misunderstood the concept of constructors and initialization. Take a look here: http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html for more info.

  Note that if you are to accept an answer accept either objects' or CEHJ's.
0
CEHJCommented:
>>you can also pass them in the constructor as CEHJ suggested but it is practically pointless

It is not 'practically pointless'. You seem to be making the same assumptions as objects about how this applet is to be used in this context ;-) I have seen numerous examples in this TA where people want their applet to double as essentially a Panel. Where it is used in this way, it is perfectly legitimate to pass a String to the constructor.

The most you can say is that in the *general* context usage of an applet, i.e. in a browser or appletviewer, this constructor will be ignored, as per my comments at Date: 12/02/2003 07:44PM PST
0
girionisCommented:
>  I have seen numerous examples in this TA where people want their applet to double as
>essentially a Panel

  You might as well use a value object instead of an applet then.

  Anyway I do not want to start a flame whether or not is appropriate to use a non-empty constructor in an applet but we have to take into account that applets are a "special" kind of java programmes and it is always advisable to defer from creating them using the "new" keyword.

  Both suggestions are correct in my opinion.

 
0
CEHJCommented:
>>You might as well use a value object instead of an applet then.

Again you seem to be making an assumption - that it will only be used in one context and that it doesn't need/ cannot employ, dual behaviours ;-)
0
CEHJCommented:
>>
but we have to take into account that applets are a "special" kind of java programmes and it is always advisable to defer from creating them using the "new" keyword.
>>

I have already taken that into account and mentioned the appropriate provisos.
0
objectsCommented:
> dual behaviours ;-)

your code will not even function as an applet.
0
objectsCommented:
You can use the following to pass a string to your constructor:


Sender s = new Sender("sd");

public class Sender
{
   private String S = null;

   public Sender(String sender)
   {
      S = sender;
   }
}
0
CEHJCommented:
>>
public class Sender
{
>>

I thought this was meant to be an Applet?
0
objectsCommented:
> I thought this was meant to be an Applet?

Make up your mind :-D
0
CEHJCommented:
It's you that needs to make your mind up. Your first code acknowledges that irish_paddy wants an applet. Now it seems to be something different...
0
objectsCommented:
I am simply responding to irish_paddy's question.
0
objectsCommented:
And the applet code is also posted above if an Applet is what is required.
0
objectsCommented:
What exactly is your objection to me posting example code related to the question anyway? It's intended for irish_paddy not you.

0
CEHJCommented:
I have no objection whatsoever - post away!
0
InNoCenT_Ch1ldCommented:
irish_paddy>> Im a dummy in Applet so i couldnt really help u in Applet.. but to create an object and pass a parameter to the class to initialize it looks sth like:

public class A
{
  public A(String title)             //constructor of A, accept a String as parameter
  {
     System.out.print(title);
  }
}


public class B
{
  A aaa = new A("Unknow");   //create an instance of A

  //class B code here
}

*for your applet's problem, you can get help from either objects or CEHJ...

-Ch1ld-
0
InNoCenT_Ch1ldCommented:
>>I want to pass a string to a new class like so,
I cant see why the followiing code wont work...

your example:
     Sender s = new Sender("sd");

//a seperate file
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.applet.*;


public class Sender(String sender) //<error here, see my example
{
   
}

-------------------------------------------------------------------------------------------

     Sender s = new Sender("sd");

//a seperate file
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.applet.*;


public class Sender //corrected version
{
   //constructor
  public Sender(String sender)
  {

  }
}
0
irish_paddyAuthor Commented:
Forgot i'd this open, its workin now anyway, thx
0
objectsCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.