Solved

How do i use the static fields in set methods

Posted on 2011-03-22
9
285 Views
Last Modified: 2012-05-11
Where am i doing wrong? What should I change?My source code:

My interface file has:
public interface Stud{
public static String name = null;
public static Sting ID = null;

public info getinfo(String usrname, String usrID) throws SQLException;
public void setName(String newName);
public String getName();
public void setID(String newID);
public String getID();
}

My implementation file has:
public class student implements Stud{
@Override
public info getinfo(String usrname, String usrID) throws SQLException{
Stud.name = usrname;   // get error: final field Stud.name = usrnamecannot be assigned
Stud.ID = usrID;   // get error: final field Stud.ID = usrID cannot be assigned
}
public void setName(String newName){
Stud.name = usrname; // get error: final field Stud.name = usrnamecannot be assigned
}
public String getName(){
return Stud.name;
}
}
0
Comment
Question by:siddharthkool
  • 4
  • 3
  • 2
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35195654

Why do you need to declare name and ID static?
They are not class specific, they are instance specific.

It complains that it cannot assign value to final variable


I don't understand either method public info getinfo ... dose not return type info

In setName  variabe usrname is not defined
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35195662
In the interfaces you can declare only constants
You can check this discussion on this subject:
http://www.coderanch.com/t/408233/java/java/Instance-variables-interface
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35195672
0
 
LVL 92

Expert Comment

by:objects
ID: 35195682
> public static String name = null;
>  public static Sting ID = null;

they should not be static, and they shouldn't be in your interface
move those lines to your implementation and remove the static keyword

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:siddharthkool
ID: 35195797
If I were to define variables in interface then it should be public static final var = null;
and then should methods in interface also be static?
for example:

public interface Stud{
public static final String name = null;
public static final Sting ID = null;

static public info getinfo(String usrname, String usrID) throws SQLException;
static public void setName(String usrname);
static public String getName();
static public void setID(String usrID);
static public String getID();
}

public class student implements Stud{
@Override
static public info getinfo(String usrname, String usrID) throws SQLException{
Stud.name = usrname;   // get error: final field Stud.name = usrnamecannot be assigned
Stud.ID = usrID;   // get error: final field Stud.ID = usrID cannot be assigned
}
static public void setName(String usrname){
Stud.name = usrname; // get error: final field Stud.name = usrnamecannot be assigned
}
static public String getName(){
return Stud.name;
}
}
0
 
LVL 92

Accepted Solution

by:
objects earned 25 total points
ID: 35195807
you can't have static methods in the interface
making everything static doesn't really make sense. why are you making them static?
0
 
LVL 92

Expert Comment

by:objects
ID: 35195810
they also shouldn't be final (if you want to have setters)
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 25 total points
ID: 35195829
And those are properties of individual instances, say name of individual student,
they can't be static, as static variables have the same value for all instances of the class
What is the reason that drives you to make them static?
0
 

Author Closing Comment

by:siddharthkool
ID: 35196008
solved
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json example 39 135
servlet requestdispatcher include and forward differences 1 25
servlet example issue 6 33
How to convert String matching to regex in java 4 21
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 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