Super- first statement in constructor.
my compiler (Jcreator LE) doesn't seem to recognize that super is the first statement in the constructor, and gives me an error message (the same thing happens using the this keyword).
public void Student(String str, int day, int mo, int yr, String maj)
super(str,day,mo,yr);
major=maj;
return;
With that code as the constructor, I get this error message
C:\Java\JCreator le\MyProjects\Lab 5\Student.java:8: call to super must be first statement in constructor
super(str,day,mo,yr);
If anybody has any ideas why this is happening or what I can do to correct it, I would appreciate it.
Thanks,
Gareth
You are trying to call a method.
Instead you have to use super( ) to controll the construction
of a parent class part of an object.
Code is something like this:
class Student {
public Student(String str) {
// initialize this object using str
public Student(int day) {
// initialize this object using day
class FromStudent extends Student {
public FromStudent(String str) {
// pass control to Student constructor
super(str); // the first constructor here
public FromStudent(int day) {
// pass FromStudent to Student constructor
super(day); // the first constructor here
Similar Messages
-
Again on "call to this must be first statement in constructor"
Hi, I've spent the last hour reading tons of docs & threads about the "call to this must be first statement in constructor" problem, but I still cannot work it out.
Here is the code
public Orologio(String s)
StringTokenizer exploder=new StringTokenizer(s,":",false);
this(Integer.parseInt(exploder.nextToken()),Integer.parseInt(exploder.nextToken()));
}Well, I know this..() must be the first line in the method, but I need to parse my input string before passing the parameters to the Orologio(int,int) constructor!
Thankyou!You could define an initialization method:private void init(int a, int b) {
... your initialization code that was in your constructor...
public Orologio(String s) {
StringTokenizer exploder=new StringTokenizer(s,":",false);
init(Integer.parseInt(exploder.nextToken()),Integer.parseInt(exploder.nextToken()));
public Orlogio(int a, int b) {
init(a, b);
}However, make sure it is the responsability of this class to handle String as constructor parameter. Maybe you should force the client to tokenize the string and provide integer values to the constructor (and then get rid of the Orologio(String s) constructor.) -
Call to super must be first statement in constructor
Why i've receive this message if the code of the constructor is that:
public void Angulo(String Base){
super(Base);
The call to superclass constructor is the first statement on this function.
Can somebody helpme? ThanksHi,
This is a regular function and not a constructor. If it is, then it should be
public Angulo(String Base){
super(Base);
Hope this helps,
Kurt. -
"call to this must be first statement in constructor"
Here's the code:
public class TAGEmail {
private StringBuilder sTO = new StringBuilder();
public TAGEmail()
public void setTO(String sRecipient)
this( sRecipient, true );
public void setTO(String sRecipient, boolean bAppend)
String sRegexPattern = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$";
Pattern oPattern = Pattern.compile(sRegexPattern, Pattern.CASE_INSENSITIVE);
Matcher oMatcher = oPattern.matcher(sRecipient);
if( ! oMatcher.find() )
oLogger.addEntry(2,"Email address '" + sRecipient + "' is not a valid e-mail address!");
if( sTO.equals(null) || bAppend )
sTO.append( "\"" + sRecipient + "\"" );
else
sTO.delete( 0, sTO.length() );
sTO.append( "\"" + sRecipient + "\"" );
}The part I don't get is that it's not in a constructor...and it is the first line of the method it's in. Am I missing something?Ah. My thought process was just the opposite. If
the compiler is going to add it anyway, I'll just do
it now. Symantics, I suppose.Well, the compiler doesn't add it into the source code. It simply interprets the two expressions identically. (It might be possible that something is hiding or shadowing the method so that this. is necessary for disambiguation. I've only seen that with variables though. I don't know if it can ever be an issue with methods. If it happens, I'd say something is poorly named.) -
Call to super must be first statement
I'm new to Java and I have a question regarding super(). I'm implementing a design where we have a custom exception which extends Exception. The design calls for null messages to lead to throwing an IllegalArgumentException, but Exception's constructor does not throw an IllegalArgumentException for a null message.
Here is what I want to do, but not allowed to since super must be the first statement in the constructor:
public CustomException(String message) {
if (message == null) {
throw new IllegalArgumentException("message cannot be null.");
super(message);
}I could call super first and then throw an exception if the message is null, but that doesn't seem like a good solution.public CustomException(String message) {
super(nullCheck(message));
static String nullCheck(String message) {
if (message == null) {
throw new IllegalArgumentException("message cannot be null.");
return message;
} -
Any idea why super() and this() needs to be first statement
Java recommends super() and this() to be the first statement to be used.why this design constraint is imposed?
This imposes one more restriction
where super() and this() cannot be used at the same time?
how this constraint is useful.
for eg:
class A
A(int a)
System.out.println("A(int)");
A(int a,int b)
System.out.println("A(int,int)");
class B extends A
B()
super(1);
System.out.println("B()");
B(int i)
this();
super(1,1);//compile-error :must be first statement
the above eg may be a bad design,but are the constraints imposed for this purpose?or anything else?To make sure that the constructor is the first thing that runs, and to make sure >the super instance is completely constructed before we do anything with the >child instance.
Both these go toward having the object be in a consistent, valid state.I accept the point,but considering the code part
B()
//super(1);
System.out.println("B()");
B(int i)
super(1,1);
this();
}in above super instance will be constructed first followed by this().
but this() cannot be used as second statement???
I feel ,this is because this() in turn calls the super constructor again?to restrict this constraint made?is it something like that? -
Hi.
Im running JSC created application and getting in the log file this messages:
[date & time] warning (24167): CORE3238: stderr: JdbcRowSetXImpl (setTransactionIsolation): [Sun][Oracle JDBC Driver][Oracle]ORA-01453: SET TRANSACTION must be the first statement of transaction
As you may see, Im using JSC JDBC drivers. Im running on Sun One Web Server version 6.1 sp2 on Linux RedHat 3Ent. The application was builded with JSC me with latest patch applied.
It seems a bug in the JdbcRowSetXImpl.
Has someone similar problem?
Regards.Hi John.
This message is present on all log file a few days after the application began in production use, almost a month ago.
The only change done in the configuration few days after the app was deployed was "Guarantee Isolation Level" in the JDBC connection pool configuration, switched from OFF to ON.
Yesterday I turn OFF again and have no more messages about SET TRANSACTION...
I guess that the SET TRANSACTION is issued two times, one by the web server before giving the connection to the app then by the row set impl.
I will let you known if the messages appears again (I guess not).
Regards. -
Two statements, get results from the first statement?
Ok maybe I made it sound a little harder than it is, but this is what I am trying to do. I have a result set from from a database call when I pass in the username and password . In the 1st call I get the Users info: User_ID, first_name, lastname, etc...
But I either need to get info from the first statement, and pass it to the 2nd statement or just do two statement calls calling the same info and passing it to my user object? I dont know which if any is faster, or better. The only thing I need from the first statement is the user_id. In the 2nd statement I need the user_id to find out the user permissions.
Here is the original code when It was getting the user info and permissions from the same table. try
dba = new DbAccess();
java.sql.Connection con = dba.getConnection();
java.sql.DriverManager.getConnection("jdbc:oracle:thin:@kares:1523:appd","user","pass");
ps = con.prepareStatement("SELECT USER_ID, LAST_NAME, FIRST_NAME, LOGIN, PASSWORD," +
"X, Y, Z FROM RD_USERS WHERE LOGIN=? and PASSWORD=?");
ps.setString(1, user);
ps.setString(2, pass);
rs = ps.executeQuery();
// set the user attributes
if ( rs.next() )
loginValid = true;
setUserId(rs.getInt("USER_ID"));
setLastName(rs.getString("LAST_NAME"));
setFirstName(rs.getString("FIRST_NAME"));
setLoginName(rs.getString("LOGIN"));
setPassword(rs.getString("PASSWORD"));
X = rs.getBoolean("X");
setX(X);
Y = rs.getBoolean("Y");
setY(Y);
Z = rs.getBoolean("Z");
setZ(Z);
else
setLastName(null);
setFirstName(null);
setLoginName(null);
setPassword(null);
setX(false);
setY(false);
setZ(false);
rs.close(); rs = null;
ps.close(); ps = null;
//dba.close(); dba = null;
} The code above gets the info from one table, I now need to get the X, Y, and Z from a different table, but I need the USER_ID first. So, the first table is set up with the user_id and user info, and the 2nd table is set up with the user_id and user permissionsThanks DrClap I started on the the query and I did it a little differently, but it should hopefull do the same thing. my tables are constructed as so:
USER
USER_ID
FIRST_NAME
LAST_NAME
USER_NAME
PASSWORD
ETC....
USER_PERMISSIONS
USER_ID
PERMISSION X
PERMISSION Y
PERMISSION Z
ETC....
My Select looks something like this
ps = con.prepareStatement("SELECT a.USER_ID, a.LAST_NAME, a.FIRST_NAME, a.USER_NAME, a.USER_PASSWORD," +
"b.X, b.Y, b.Z FROM RD_USERS a, RD_PERMISSIONS b WHERE USER_NAME=? and USER_PASSWORD=?");
This is where I come to a crossroads, Can I add on another AND to say "WHERE a.USER_ID = b.USER_ID
and do I use the =, or is it ==, or .equals(). Thanks in advance.
orozcom -
SET TRANSACTION must be first statement of transaction
I am using oracle8i db with sun one 7.0. i use the oracle thin driver to talk to the db. i have bunch of session beans which uses the oracle data source. when i invoke the session bean, i get this ORA-01453 error.
Here is my jdbc properties:
<jdbc-connection-pool steady-pool-size="8" max-pool-size="32" max-wait-time-in-millis="60000" pool-resize-quantity="2" idle-timeout-in-seconds="300" is-isolation-level-guaranteed="true" is-connection-validation-required="true" connection-validation-method="table" fail-all-connections="false" datasource-classname="oracle.jdbc.xa.client.OracleXADataSource" name="JSBContainerPool" res-type="javax.sql.XADataSource" validation-table-name="dual" transaction-isolation-level="read-committed">
<property value="jdbc:oracle:thin:@muruga:1521:DEV" name="URL"/>
<property value="muruga" name="User"/>
<property value="muruga" name="Password"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" pool-name="JSBContainerPool" jndi-name="jdbc/JSBContainerDataSource"/>
Error message:
The exception reported is: "java.sql.SQLException: ORA-01453: SET TRANSACTION
must be first statement of transaction
WARNING: CORE3283: stderr: at oracle.jdbc.dbaccess.DBError.throwSqlExceptio
n(DBError.java:168)
WARNING: CORE3283: stderr: at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.j
ava:208)
WARNING: CORE3283: stderr: at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:543
WARNING: CORE3283: stderr: at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Pro
tocol.java:1405)
WARNING: CORE3283: stderr: at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFet
ch(TTC7Protocol.java:822)
WARNING: CORE3283: stderr: at oracle.jdbc.driver.OracleStatement.executeNon
Query(OracleStatement.java:1446)
WARNING: CORE3283: stderr: at oracle.jdbc.driver.OracleStatement.doExecuteO
ther(OracleStatement.java:1371)
WARNING: CORE3283: stderr: at oracle.jdbc.driver.OracleStatement.doExecuteW
ithTimeout(OracleStatement.java:1900)
WARNING: CORE3283: stderr: at oracle.jdbc.driver.OracleStatement.execute(Or
acleStatement.java:737)
WARNING: CORE3283: stderr: at oracle.jdbc.driver.OracleConnection.setTransa
ctionIsolation(OracleConnection.java:1081)
WARNING: CORE3283: stderr: at com.sun.enterprise.resource.Jdbc20XaAllocator
$ConnectionWrapper.setTransactionIsolation(Jdbc20XaAllocator.java:196)In the connection pool setting in Admin server under the section Transaction Isolation deselect the option "Guarantee Isolation Level" .This should solve the problem.
regards
Kris -
Cannot downloading TV movies. Itune attempts download but then freezes after about 1 minute into the download. Download status first states video will take 37 then 45 hours to download. My computer more than meets the minimum requirement for HD downloads...not sure about the HDCP capability
Hi 1611kjb,
Please refer to the solution mentioned in the following threads :
http://forums.adobe.com/message/5396792#5396792
http://forums.adobe.com/message/5409050#5409050 -
How to capture First Statement
Hi All
I want to search some key words like Search and condense used in programs, all programs i am reading in internal table one by one (itab).
example
loop at itab.
itab-text CS 'SEARCH '.
i am looking for the text which starts with Search means
Search a in b.
.......... search a in b.
..................... search a in b.
cosider dots as a spaces (blanks, take it like it is written after some offset)
any format in particular line where it should starts with search.
and i want to avoid text like
loop at itab " for search a in b.
or any thing which is not satrts with search key word as well when its present after some letters also i want to avoid.
i mean to say that in particular line of program which should starts with Search first character should be search may be it is after some spaces.
pls suggest how to achieve this
thanks in advance.hi,
Use
SCAN ABAP SOURCE OBJECT KEYWORDS INTO T_KEYWORDS..
.etc.
Using the above statement you can have all the keywords in a program into the internal table t_keywords. Try with F1 for more information.
Using SCAN you can even check for INCLUDES and also any Z Function modules using the SEARCH key word in your custom program.
Regards
Sharath -
Cannot comment in a view before first statement
My apologies if this is the wrong group/forum for this question.
I'm creating views using SQL Developer and noticed quickly that under some circumstances my comments were disappearing when I saved the view. What I mean is that I would save a view and the next time I went to edit it the comment would be gone. Further research revealed the following:
CREATE OR REPLACE MY_VIEW
AS
-- THIS COMMENT WILL DISAPPEAR WHEN I SAVE THIS VIEW
SELECT *
FROM MY_TABLE
CREATE OR REPLACE MY_VIEW
AS
SELECT *
-- THIS COMMENT WILL NOT DISAPPEAR WHEN I SAVE THIS VIEW
FROM MY_TABLE
Can you really not have any comments before the first keyword in a view?
Any information you can share will be appreciated.
CarlHi Carl,
Another way with DBMS_METADATA.GET_DDL, e.g,
SQL> set serveroutput on
SQL>
SQL> CREATE OR REPLACE VIEW vw AS (
2 -- some comment
3 SELECT
4 dummy
5 FROM dual
6 );
View created
SQL> begin
2 dbms_output.put_line( dbms_metadata.get_ddl('VIEW','VW','SYS') );
3 end;
4 /
CREATE OR REPLACE FORCE VIEW "SYS"."VW" ("DUMMY") AS
-- some comment
SELECT
dummy
FROM dual
PL/SQL procedure successfully completedRegards,
Christian Balz -
Is Imports NationalInstruments.DAQmx still the first statement in a VB2010 programme?
I had good results with VB2005 and WindowsXP but having upgraded to Window7 We need to use VB2010 to operate the USB6008.
When I type Imports NationalInstruments.DAQmx at the very top there's a sqiggly underline in green with pop-up to the effect that this is the wrong Namespace or the file cannot be found.
I remain in due course corrected please.jeffrey123 wrote:
I had good results with VB2005 and WindowsXP but having upgraded to Window7 We need to use VB2010 to operate the USB6008.
When I type Imports NationalInstruments.DAQmx at the very top there's a sqiggly underline in green with pop-up to the effect that this is the wrong Namespace or the file cannot be found.
I remain in due course corrected please.
When you installed the NI-DAQmx driver software, was Visual Studio 2010 already installed?
If not, you may need to go back to and repair the install or re-install of NI-DAQmx to add support for .NET 4.0
I only have VB.NET 2010 Express. Once I added the reference to NI-DAQmx, C:\Program Files (x86)\National Instruments\MeasurementStudioVS2010\DotNET\Assemblies\Current\NationalInstruments.DAQmx.dll, everything was fine. -
Catching exception of a super constructor
Is it possible to catch the exception thrown by a super class
constructor in a sub clas constructor?
public class A
public A() throws Exception
throw new Exception("Exception from A");
class B extends A
public B()
try
super();
catch(Exception e)
}I get an error saying,
A.java:16: call to super must be first statement in constructor
super();
^
Any thoughts?More on this here,
http://archive.devx.com/free/tips/tipview.asp?content_i
=2384&Java=ONThanks for that link. I completely agree to what it says.
In other words, not being able to successfully construct our super class implies that we cannot create our derived class.
But I feel this must be mentioned clearly somewhere in JLS. And the error message that we get must be "You cannot catch exceptions thrown by a super class constructor". -
Newb question re: JApplet
heya,
i have the following applet that is damned to work. any advice as to why:
a) the JLabel and JTextField aren't displaying properly(the label doesn't show at all)
b) the "\n" escape doesn't create a new line - rather, an empty square indicating an escape seq.
c) the applet doesn't clear its' contents upon reciept of a new action event.
basically, the applet asks for an integer and should then display a square built of asterisks with the sides equal to the entered value. simple and basic.
any tips would be greatly appreciated. on my 3rd bottle of Advil.... =(
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawSqaure extends JApplet
implements ActionListener {
JLabel lblGetInput;
JTextField txtGetInput;
int intUserInput = 0;
public void init()
Container c = getContentPane;
c.setLayout(new FlowLayout);
lblGetInput = new JLabel("Enter an integer: ");
txtGetInput - new JTextField(5);
txtGetInput.addActionListener(this);
c.add(lblGetInput);
c.add(txtGetInput);
public void actionPerformed(ActionEvent e)
intUserInput = Integer.parseInt(txtGetInput.getText());
repaint();
public void paint(Graphics g)
squareOfAsterisks(g, intUserInput);
public void squareOfAsterisks(Graphics g, int side)
String output = "";
for (int row = 0; row < side; row++) {
for (int column = 0; column < side; column++) {
output += "* ";
output += "\n"
g.drawString(output, 20, 40)
}again, any tips/suggestions any of you have would be greatly appreciated. thanks
miriromkk,
i included the call to the superclass constructor super(g] as suggested, however, i kept getting a compile error stating call to super must be first statement in constructor.
i was able to rewrite the custom method squareOfAsterisks() so that it would display the shape properly. yet the applet still won't show the JLabel, nor will it clear the square previously drawn when a new value is entered.
the code changes are below
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawSquare extends JApplet implements ActionListener{
JLabel lblGetInput;
JTextField txtGetInput;
int intGetInput;
public void init()
Container c = getContentPane();
c.setLayout(new FlowLayout());
lblGetInput = new JLabel("Enter an integer: ");
txtGetInput = new JTextField(5);
txtGetInput.addActionListener(this);
c.add(lblGetInput); c.add(txtGetInput);
public void actionPerformed(ActionEvent e)
intGetInput = Integer.parseInt(txtGetInput.getText());
repaint();
public void paint(Graphics g)
//super(g) causes error
squareOfAsterisks(g, intGetInput);
public void squareOfAsterisks(Graphics g, int side)
for (int row = 0; row < side; row++) { // columns loop
for (int column= 5; column < (side + 5); column++) { // rows loop
g.drawString("*", row * 10, column * 10);
}in addition, i created another applet that has not GUI components. the data is entered via a JOptionPane. still the same result (less the label problem since there isn't one) -- the applet doesn't "clear" what was drawn before.
ugh.. i never imagined this as being as difficult as its turned out to be. thanks for your suggestion Trev =)
Maybe you are looking for
-
Control of Outbound SMTP?
Hi, Is it possible to control where users can send mail and direct them to specific relays? I only want a couple of people to be able to mail out to the internet and ideally they would also have attachment stripping etc. running and others should onl
-
I received a notice of a security/stability update to FireFox 3.6.3. I have a MacBook Pro 13" running OS 10.6.4. The claim is that I do not have permission to upgrade and I should consult with my system administrator. I am the administrator of this m
-
PHYSICAL PAGE SIZE IN REPORT DESIGNER 2.5
I have designed a report in character mode and it prints on a page size of 15" * 5.5". Only one record is printed on one page. I have set the page size at design time and looks fine. I have also defined page as 130 * 33 characters. But when you run t
-
Hi, We have a strange case in the self service... we set up our approvers dynamically using calling to external function. it was working fine but currently we found out one or more case that the workflow repeat the approvers. Let's say that the trans
-
I am not able to mail the photos from iPhoto. Its always saying that the email address it's not recognized. Another situation is that when I try to add my me account in the preferences account, doesn't recognize my @me password. Tks for any help! :