Solved

How do i use the static fields in set methods

Posted on 2011-03-22
9
288 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 49
web application structure 18 77
spring jars download 1 27
Java: anonymous class 4 20
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

773 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