Solved

How do i use the static fields in set methods

Posted on 2011-03-22
9
289 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 21
JavaFX TableView not displaying correctly 3 55
Java Restore security prompts not working 10 56
Java: The Public Class Main 4 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

840 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