Solved

simple error

Posted on 2003-12-02
30
274 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
  • 13
  • 11
  • 2
  • +2
30 Comments
 
LVL 92

Accepted Solution

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

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 25 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now