This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes about  super key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "about  super key" Watch "about  super key" New topic
Author

about super key

Kranthi Kondapaka
Ranch Hand

Joined: Sep 17, 2007
Posts: 31
package pack;
import java.io.*;
import java.util.*;
class Samp1
{
public Samp1()//default constructor
{}
String name;
int empid;
float sal;
public Samp1(String name,int empid,float sal)
{
this.name=name;
this.empid=empid;
this.sal=sal;
}
public void disp(String name,int empid,float sal)
{
System.out.println("Name of the Employee:"+name);
System.out.println("Employee ID of the Employee:"+empid);
System.out.println("Salary of the Employee:"+sal);
}
}
class SubSamp1 extends Samp1
{
public static void main(String[] args) throws IOException
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the EmpId:");
int empid=Integer.parseInt(br.readLine());
System.out.println("Enter the Name:");
String name=br.readLine();
System.out.println("Enter the Salary:");
float sal=Float.parseFloat(br.readLine());
super.Samp1(name,empid,sal);
}
}
In the about program i am getting an error at super key and the error is

"Cannot use super in a static context ".
Can anyone give me the solution for this problem
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You are trying to call a constructor from a static context. You need to create an instance of your class to call the constructor.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Kranthi Kondapaka
Ranch Hand

Joined: Sep 17, 2007
Posts: 31
Hi,
but its the subclass of samp1,then what is the need to create the instance when m extinding the superclass.

According to my knowledge when we extend a class then we can directly use the methods and variable of the base class unless they are declared private
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Beginner question.

super.Samp1(name,empid,sal);


Samp1() is the constructor for Samp1 class. Constructors cannot be called directly, as if they were ordinary methods. A constructor can only be called indirectly, by using the "new" operator.

What are you trying to achieve with this line?


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


but its the subclass of samp1,then what is the need to create the instance when m extinding the superclass.


Since you only have the constructor you want to use in your super class you will need to either create an instance of the super class:


or add a simmilar constructor to your subclass and call that.



According to my knowledge when we extend a class then we can directly use the methods and variable of the base class unless they are declared private

Well, that's partically true. A couple of points though: a constructor is neither a method or a variable. It is a special way of creating an instance of an object. Also, since a constructor is called to create a specific instance you can't use them in a static context. "static" remember applies to the class, not the object instance.
Kranthi Kondapaka
Ranch Hand

Joined: Sep 17, 2007
Posts: 31
Thanks guys(especially paul),its basic thing but i got good refreshment of my knowledge.
 
Consider Paul's rocket mass heater.
 
subject: about super key