Solved

How do i use the static fields in set methods

Posted on 2011-03-22
9
282 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

12 Experts available now in Live!

Get 1:1 Help Now