When I run static analysis on the following code:
public ExtractDBScripts(String resBundleName)
{
super();
...
|
Java requires that if you call this() or super() in a constructor, it must be the first statement. Why?
For example:
public class MyClass {
public MyClass(int x) {}
}
public class MySubClass extends MyClass ...
|
class A{
A(int x){}
}
class B extends A{
B(int x){}
public static void main(String args[]){
...
|
I have a class A and write a subclass B. A has only one constructor which is parameterised. B has to call this super constructur of A.
Now I want to use ... |
isnt this one automatically put by the compiler if i don“t put it in a subclass's constructor?
that means i dont even should care about it? in some articles they put it ... |
Given that I have a class Base that has a single argument constructor with a TextBox object as it's argument. If I have a class Simple of the following form:
public class ...
|
This is a question about coding style and recommended practices:
As explained in the answers to the question unnecessary to put super() in constructor?, if you write a constructor for a ... |
|
I am having a class 'ClassA' which is having private constructor.
public final class ClassA{
private ClassA{
}
public static void main(String[] arg) }{
;
;
...
|
import java.io.*;
class MyException1
{
static String str="";
public static void main(String args[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your food");
try{
str=br.readLine();
}catch(IOException e){
System.out.println("Exception has been occurred"+e);
}
try{
checkFood();
}catch(BadException be){
...
|
Please explain
public class Contact {
private String contactId;
private String firstName;
private String lastName;
private String email;
...
|
What does the super method do?
public DataFetch (Context context){
super();
this.ctx=context;}
Does this constructor make the context of ... |
I have an array at actionTable.get(state).
When I go to add an onject to the array, namely the Reduce, the properties of the reduce don't seem to go with it.
The array is ... |
The following code is a testing program. Why i can't use A(int a), in this program?
public class A {
int a;
void meth(int b) {
...
|
i have created two classes Standard and Family which extend abstract class Room, when compiling the Standard class i am met with the error "cannot reference roomNumber before supertype constructor has ... |
A call to super() which has to be the first statement in the constructor, creates [gross oversimplification] that part of the object which is inherited. In most subclasses a super() call is essential. A new Superclass() call creates a new object of the superclass completely separate from where one is working at present. |
Hi i am naveen i am new to this forum.Last week i have attended for one interview in that they asked me that is it possible for us to a create sub class object by calling super class constructor.If any body nows this answer please reply to me. thanks, naveen. [ February 03, 2008: Message edited by: naveen maddala ] |
Hi I'm doing a project in Java I've run into a problem. I have a base class with 2 final variables. class ClassParent { // the two final variables private final Object cons1; private final Object cons2; //constructor for cons cell protected ClassParent(Object a, Object b){ cons1 = a; cons2 = b; } DerivedClass extends ClassParent{ //constructor for DerivedClass //only initializes ... |
Hi. implicit calls are made for you. that's why they're called implicit. this means that, suppose your superclass has a default constructor - it will be called by the extending class's constructor (unless an explicit call is made). explicit calls are made by you, as the programmer. when you write your constructor, and want to call a specific constructor of the ... |
I have the following code: class Snack extends VendingMachinePoly { private double cost; private int calories; // Parameterized constructor Snack(double c, int cal) { super(50, 50.0, c); cost = c; calories = cal; When I compile the program I get an error that states: "Snack.java": cannot resolve symbol: constructor VendingMachinePoly (int,double,double)in class chapter9.VendingMachinePoly at line 26, column 5 However, the following ... |
|
Well I took some good advise and started my program over and did 1 line at a time to fix errors and not end of with a bunch. But now I got a problem. CheckingAccount has no errors but trying to compile CheckingAccountPlus I get this error revolving around Keyword "SUPER"? Also included is partial Test class, which I stopped with ... |
The constructor of class B will implicitly be called when you create a new object of class C. When you create a new object of class C, the following will happen: The constructor of class C will call the constructor of class A, which in turn will call the constructor of class B. Note that a subclass constructor will call the ... |
Since all super types of your class might have internal fields, they have to have one of their constructors run. These super constructor runs have to be completed before your first assignment within your constructor. This makes perfectly sense. Suppose you could place the super() call anywhere within your constructor and a super type had protected member variables which are to ... |
As far as I know you can not put code before the call to super(). The call to super is added automatically to your constructor unless you specifically put the call there. The main reason to do this is if you need to call super with some parameters or if there is no parameter-less constructor. *third try's a charm....* |
Hi, Here is a piece of code and its output. Could not understand why the derived class method is called: class Test1 { public Test1() { System.out.println("In ctr of Test1()"); d(); } protected void d() { System.out.println("In Test1.d()"); } } public class Test2 extends Test1 { public int i = 2; public Test2() { System.out.println("In ctr of Test2()"); } protected void ... |
Well, it's possible to chain multiple ? : together. But many people would consider that hard to read. You can also call a static method as part of the super constructor call: public MyClass() { super(method(foo)); ... } private static int method(int foo) { ... } Put whatever you want inside the method, as long as it doesn't access instance variables. ... |
public class GraphicalDice extends PairOfDice { public GraphicalDice() { super(){} // This super class constructor gets called automatically, if nothing is specified in first line super(3,4); // Call the constructor from the // PairOfDice class, with parameters 3, 4. initializeGraphics(); // Do some initialization specific // to the GraphicalDice class. } super(7, 8); //can I make call like this ? . ... |
I have extended a BaseClass to SubClass. In BaseClass, I have defined parameterized consstructor, but there is no non parameterized constructor defined. Since I have defined parameterized constructor, non parameterized constructor will not be created automatically and I am required to define it. If I dont create a non parameterized constructor in BaseClass and extend this BaseClass to SubClass. If I ... |
|
public class SuperTest{ public SuperTest(){} public SuperTest(int i){} public void testMethod(){ System.out.println("This is test method in super class"); } } public class SubTest{ public SubTest(){ super.SuperTest(); } public void testMethod(){ System.out.println("This is test method in sub class"); SuperTest sup = new SubTest(); sup.testMethod(); } } When I run this code it prints output "This is test method in sub class" Why ... |
hi all, Let us say that we have a class A as follows. Class A { private int a; public int b; } And class B, which extends class A. Class B extends A { int c; } Now, if I create an object for B, then will it be having the member variable 'b' as we have inherited? The answer ... |
Since you are calling a constructor of a class using super() or this(), you don't have an object of that class yet.. So, you don't have access to instance variables or methods of that class. So, while instantiating a class using super() or this(), you have access only to their static members since they are loaded when jvm loads the class. ... |
Hi all, why do we have to call the super class constructors in order to use the instance variables and methods of sub class.Even if we want to use the instance variables inside the constructor of the sub class,we cannot do so unless we have the constructors of all the super classes run already. Why is so? Why is it necessary ... |
class Atom { Atom() { System.out.print("atom "); } } class Rock extends Atom { Rock(String type) { System.out.print(type); } } public class Mountain extends Rock { Mountain() { super("granite "); new Rock("granite "); } public static void main(String[] a) { new Mountain(); } } The output of the code is : atom granite atom granite when Mountain constructor is executed the ... |
|
|
First of all, please don't ever have a class named "super". Also, many of your fields and arguments are missing types; I'm assuming that this is not your actual code. As to your actual question, what you put in your classes depends on what the classes are supposed to be about. So in your "super" class, you put in fields and ... |
|
In addition to what the others have said, there's no reason to pass this to a constructor. The c'tor already has a this, and it's referring to the same object. There's just one object, not a separate parent and child object. (Though, as already pointed out, it doesn't make any sense for Receiver to extend Sender. A Receiver is not a ... |
Hi, Could someone please explain why it is not allowed to explicitly call this() or super() in a constructor body anywhere as opposed to the first statement in the constructor (which in turn implies that this() and super() can not be used together) ? Also, If the constructor is a constructor for an enum type, it is a compile-time error for ... |
public static void main(String[] args) { Cena2 x = new Cena2(); x.morder("Alguien"); } } As they see treatment use the functions inheriting them, but it leaves the following error to me: **Implicit super constructor Galleta() is undefined. Must explicitly invoke another constructor. In this line: public Cena2() { System.out.println("Haciendo una galleta de chocolate..."); } I do not understand very well to ... |
Thank you. This piece from the link you specified: [quote] Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is ... |
When you are calling super() in your child class, you are calling the super class of child, which is ONLY parent 2. It goes directly to Parent2's constructor, which ONLY takes 5 arguments, 4 ints and a String data type. So in the child class, you would have to call super(int arg1, int arg2, int arg3, int arg4, String arg5) Otherwise, ... |
|
|
Hello tivrfoa, it's the only way to initialize the (including but not limited to private) state of the super class fields without knowing the internal workings of the implementation. Also, if you were to initialize the super class state, it would break capsulation; you could not make changes to a class without potentially breaking subclasses. With kind regards Ben |
In response to topic: this is a keyword that refers to the current object. So if your code is this(x) then it will call another constructor in your class and pass the value of the x variable to it. The keyword super is used to refer to the parent class. So if you code is super(x) then it will call a ... |
|
|
|
The first statement of a constructor can be one of the following three things: 1) an explicit call to another this( ... ) 2) an explicit call to super( ... ) 3) something else in case 3) the compiler inserts an implicit call to super(), i.e. a call to the constructor of the super class without any parameters. If no such ... |
You can do this using byte code. (not advised!!) However if you feel you want to move this() or super() there is another way to do what you have in mind. One thing you can do is call static method in the this/super line which means you can execute code before this/super is called. |
|
|
private static final String[] VEX_MSGS = { "File type not supported", "Structure version not supported", "Filename format is wrong" }; int code; public ValidationError(int code) { this.code = code; } public String getMessage() { if ((code >= 0) && (code < VEX_MSGS.length)) return "error code " + code + ":" + VEX_MSGS; else return "error code " + code + " ... |
Hi All, I tried to use Super and this keyword in same constructor but it is not working.It gives the exception like constructor call must be the first. I know in base class's constructor super class's constructor should be first statement but also can we not use same class's constructor as second statement in the constructor.? Thnkx |
You have probably subclassed an object. If you dont call the superclass constructor, Java makes an implicit super() call. The super class probably doesn't have a noarg constructor defined. I am guessing you have defined a constructor in the super class that takes an argument. If you hadn't Java would have added an implicit constructor Just add a no arg constructor ... |
|
I wrote a class and a subclass - both immutable. The superclass only has one constructor and the subclass also has one constructor (though it is different than the superclass). I need for the subclass constructor to take some numbers, perform some operations and call the the constructor of the superclass. Of course this wont work because super() has to be ... |
if you want do not do it! it will not really effect anything. over there you should be just calling the default constructor of the object class. Now in other cases, that is, when the class is extending some other object you could have something as follows: public A(String name){ super(name) // continue... } I feel it is usefull sometimes and ... |
I am using third party libraries to do my development. The base class I am extending my class from does not have any constructors (with or without arguments). So, during compilation I get the following error message "Implicit super constructor Foo() is undefined. Must explicitly invoke another constructor." So, I declared a new constructor with argument in my class but still ... |
Hi, Ive got 3 classes and I need to resolve constructor of superclass of superclass: class A{ A(i){} } class B extends A{ B(i,d){ super(i) } class C extends B{ C(i,d){ } I need to resolve constructor from A class inside a constructor of class C. How can I do it? Can you help me here:) |