Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do i use the static fields in set methods

Posted on 2011-03-22
9
Medium Priority
?
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 100 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 100 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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ā€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

610 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