?
Solved

simple error

Posted on 2003-12-02
30
Medium Priority
?
287 Views
Last Modified: 2010-03-31
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.

0
Comment
Question by:irish_paddy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 11
  • 2
  • +2
30 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 9863666
public class Sender extends Applet
{
}

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 9863761
If you wanted a constructor in there too:

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

0
 
LVL 92

Expert Comment

by:objects
ID: 9863811
Passing a parameter to an Applet is pointless and that code will not even load.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 86

Expert Comment

by:CEHJ
ID: 9863827
Depends on how the applet is going to be used
0
 
LVL 92

Expert Comment

by:objects
ID: 9863874
there is only one way :-D
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9863890
This would be a perfectly legitimate way:

Sender sender = new Sender("MySender");
add(sender);
setTitle(sender.getStr());
0
 
LVL 92

Expert Comment

by:objects
ID: 9863903
But pointless, and really not worth commenting. It only confuses people that come here to learn.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9863922
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
 
LVL 92

Expert Comment

by:objects
ID: 9863955
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9863976
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
 
LVL 92

Expert Comment

by:objects
ID: 9864003
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
 

Author Comment

by:irish_paddy
ID: 9865414
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
 
LVL 35

Expert Comment

by:girionis
ID: 9865597
 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9865706
>>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
 
LVL 35

Expert Comment

by:girionis
ID: 9865754
>  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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9865787
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9865791
>>
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
 
LVL 92

Expert Comment

by:objects
ID: 9869621
> dual behaviours ;-)

your code will not even function as an applet.
0
 
LVL 92

Expert Comment

by:objects
ID: 9869632
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9869667
>>
public class Sender
{
>>

I thought this was meant to be an Applet?
0
 
LVL 92

Expert Comment

by:objects
ID: 9869749
> I thought this was meant to be an Applet?

Make up your mind :-D
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9869792
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
 
LVL 92

Expert Comment

by:objects
ID: 9869880
I am simply responding to irish_paddy's question.
0
 
LVL 92

Expert Comment

by:objects
ID: 9869891
And the applet code is also posted above if an Applet is what is required.
0
 
LVL 92

Expert Comment

by:objects
ID: 9869904
What exactly is your objection to me posting example code related to the question anyway? It's intended for irish_paddy not you.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9869925
I have no objection whatsoever - post away!
0
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9880464
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
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9880473
>>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
 

Author Comment

by:irish_paddy
ID: 9950547
Forgot i'd this open, its workin now anyway, thx
0
 
LVL 92

Expert Comment

by:objects
ID: 9951635
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question