Why Static Classes Are Allowed In Interfaces
Oct 25, 2014
I am reading about interface and i see that classes are allowed inside interfaces which are implicitly static. Here is sample of code i created and i am able to access the static method and fields as well. Here is the code snippet.
public class TestInnerClass {
public static void main(String[] args){
Test.NestedClass.printMe();
}
}
interface Test{
static class NestedClass{
static int x = 100 ;
public static void printMe(){
System.out.println(x);
}
}
}
My question is what is the use of such static classes inside interface? If i don't have access to Foo, i can't ever invoke NestedClass. Whats the design usage?
View Replies
ADVERTISEMENT
Mar 29, 2014
I did research again....
interface:
methods - abstract, default, static ONLY(abstract methods have no body, while static and defaults do, right?)
fields - public, static, final ONLY
abstract class: a normal class, but has at least one abstract method
methods - all
i.e., static, non-static, abstract (can it have a default method?)
fields - all
i.e., public, protected, private / final, non-final / static, non-static
View Replies
View Related
Jun 16, 2010
Why can't we have static methods in an interface?
View Replies
View Related
Jun 20, 2014
When are we allowed to nest interfaces inside a class? Would this be possible? Why or Why not?
View Replies
View Related
May 8, 2014
I have three classes of object, most of which must implement two out of three interfaces. The interfaces look like this:
public interface Source {
public void startSending();
} public interface Sender {
public void setReceiver();
[Code] .....
That works fine, but I am wondering if pairing the interfaces into subinterfaces is a defensible methodology. For example, all classes that act like Producer must implement both the Source and Sender interfaces. And all classes that act like Relayer must implement the Sender and BlackHole interfaces. I could define two subinterfaces like this:
public interface Factory extends Source, Sender {
}
public interface Modifier extends BlackHole, Sender {
}
I could then define my classes like this:
public class Producer implements Factory {
}
public class Relayer implements Modifier {
}
public class Consumer implements BlackHole {
}
Within the class definitions, it makes no difference, as I will have to implement the same methods either way. But it seems more self-documentary to create the subinterfaces from their parent interfaces and name them in ways that reflect what the classes that implement them must actually do.
View Replies
View Related
Apr 6, 2014
I am trying to figure out how I can most easily make it easier to make new types of units in my game. I have buildings and ships, and would like to know how I could make it easy to add new units. I have been recently told about interfaces, and have worked with inheritance a little bit.
What I would like to able to do is have it so that all of the variables and methods common to all ships could be stored in a superclass or interface, and same with the buildings. I would also like to be able to assign behaviours to the buildings and ships, maybe as interfaces, which could contain all of the methods and variables required for the functions of that ship or building.
For example, creating a new type of building that can shoot, build ships, and can regenerate nearby ships. So it would possible inherit all of the variables and methods common to all buildings, such as health, image, x, y, getX(), getY() etc. But it would then also gain the variables and methods essential for its functionality, such as shootRange, shoot(), regenRate, etc.
How could this best be achieved?
View Replies
View Related
Dec 1, 2014
While reading the design patter book, i got one doubt ,There is a List an interface having sub classes ArrayList, LinkedList etc.,
Q1) My question is Why they declared the List as interface rather than Abstract class?
Q2) i read some site -
List l = new ArrayList(); Why it is GOOD line?
ArrayList l = new ArrayList() ; Why it is BAD line?
Answer required with detailed information for Q1 and Q2.
View Replies
View Related
Apr 16, 2015
how come you can call non static methods from other classes(objects when they are created from main) but not static methods in the same class as the main method??
example I cannot call the method maximum from the main method aslong as its not static BUT i can call other objects non static methods from main??
class test{
public static void main(String [] args){
Scanner input = new Scanner(System.in); //create new Scanner object
//for input
int number1;
int number2;
[Code]...
View Replies
View Related
Feb 12, 2014
I have made a class here that has two methods. As you guys can notice, in my two methods that I made, I have listed some arguments in there with parameters. My question is that the variables im using in first method, can they be identical on my second method? Is this ok to do?
public class StudentScore {
private int math;
private int science;
private int calc;
private int history;
private int pe;
[Code] .....
View Replies
View Related
May 22, 2014
In Chapter 9, you wrote a program named MeanMedian that allows a user to enter five integers and then displays the values, their mean, and their median. Now, modify the program to throw an exception if an entered value is not an integer.If an exception is thrown, display an appropriate message and allow the user to reenter the value. Save the file as MeanMedianHandleException.java.This is what I have. The exception is caught but the program starts over asking for five new entries.
import java.util.*;
public class MeanMedianHandleException {
public static void main(String[] args) {
int sum = 0, avg, mode;
final int MAX = 5;
int[] num = new int[5];
try {
Scanner keyboard = new Scanner(System.in);
for (int i = 0; i < MAX; ++i) {
System.out.println("Enter number ");
num[i] = keyboard.nextInt();
[code]....
View Replies
View Related
Apr 21, 2014
In an JEE6 webprofile project, using JBoss EAP 6.1:
I have a CDI Bean:
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import de.dpunkt.myaktion.model.Organisator;
import de.dpunkt.myaktion.services.OrganisatorDAO;
[code]....
When I call doCreate() I get
javax.servlet.ServletException: javax.ejb.EJBAccessException: JBAS014502: Aufruf an Methode: public abstract void de.dpunkt.myaktion.services.OrganisatorDAO.save(de.dpunkt.myaktion.model.Organisator) von Bean: OrganisatorDAOBean ist nicht gestattet
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
which translates to: call of method: .... from Bean ... is not allowed
View Replies
View Related
Feb 15, 2015
The initial output of the texfile is this. NO ARRAYLIST OR COMPARATOR IS ALLOWED:
Steve Jobs 9 f 91
Bill Gates 6 m 90
James Gosling 3 m 100
James Gosling 3 f 100
Dennis Ritchie 5 m 94
Steve Jobs 9 m 95
Dennis Ritchie 5 f 100
Jeff Dean 7 m 100
Bill Gates 6 f 96
Jeff Dean 7 f 100
Sergey Brin 27 f 97
Sergey Brin 22 m 98
The collateExams method collates/sorts exam objects starting with the first 'm' (midterm) of the first object and immediately followed by the same person's 'f'(final). Only a SINGLE loop construct is allowed. The output from collateExams() should be the one below but my code is not working, i.e. collateExams method is not working. The output from collateExams() should be
Bill Gates 6 m 90
Bill Gates 6 f 96
James Gosling 3 m 100
James Gosling 3 f 100
Dennis Ritchie 5 m 94
Dennis Ritchie 5 f 100
Steve Jobs 9 m 95
Steve Jobs 9 f 91
Jeff Dean 7 m 100
Jeff Dean 7 f 100
Sergey Brin 22 m 98
Sergey Brin 27 f 97
I am getting [b]NullExceptions[/b] at
r[2*position[exams[i].getID()]+1] =
new Exam(r[i].getFirstName(), r[i].getLastName(),
r[i].getID(), r[i].getExamType(), r[i].getScore());
import java.io.*;
import java.util.*;
[Code] ....
View Replies
View Related
Jul 8, 2014
I am trying to get attachements from soap resonse,I am getting the below error "Content is not allowed in prolog" with SOAP Attachment".
soap response is
================
------=_Part_585_5017019.1404291979395Content-Type: text/xml; charset=utf-8
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body><RetrieveIncidentResponse message="Success" returnCode="0"
[Code] ....
The code i am trying is below :
try { // Create SoapMessage MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage(); SOAPPart soapPart = message.getSOAPPart();
// Iterator i=message.getAttachments();
// Load the SOAP text into a stream source byte[] buffer = soapResponse.getBytes();
[Code] .....
Detailed error log is
===============
ERROR: 'Content is not allowed in prolog.'
Jul 8, 2014 10:13:53 AM com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory createEnvelope
SEVERE: SAAJ0511: Unable to create envelope from given source
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source:
at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:114)
[Code] .....
View Replies
View Related
Jan 14, 2015
I am trying to hit the service which has been generated using apache cxf 3.0, I have generated the client using axis2 wsdl2java and whenever I tried to hit the service am getting as
"exception from OLB Service:org.xml.sax.SAXParseException: Content is not allowed in prolog."
Not sure about the cause of the issue. We have made interceptors on the server side to verify whether there is any special characters associated, but there are no such characters from provider.
View Replies
View Related
May 21, 2014
If I use an instance of Tooltip in my custom-control, it can not be imported to SceneBuilder any more. Using Tooltip in my custom-control skin works - is that the way it is supposed to be?
Background: I have a control (ValidatedTextField) which uses Tooltips to inform about unwanted characters. It works in SceneBuilder1.x.
SceneBuilder 2 does not "accept" this control unless I take out all Tooltips. I have verified this behavior with some other working controls.
I have found a workaround to use the Tooltip in the Skin.
View Replies
View Related
Mar 26, 2015
This is my code inside the method:
@Post
public static String getDetails(Representation entity) throws Exception {
String customerId = getQuery().getValues("cus_id");
}
I use this code in Restlet Representation. I try to get the value from the Request API. But I am facing the problem as "Cannot make a static reference to the non-static method getQuery() from the type Resource".
View Replies
View Related
Jun 27, 2014
From what i understand static methods should be called without creating an instance of the same class . If so why would they return an instance of the same class like in the following : public static Location locateLargest(double[][] a) , the Location class being the same class where the method is defined . I don't understand this , does it mean that every field and every method in the class must be static ? Meaning that you cannot have instances of the class because everything is static . Or it's just a mistake and the class Location cannot have a static method: public static Location locateLargest(double[][] a) ?
View Replies
View Related
Apr 26, 2015
I can't figure out what this error message "Cannot make a static reference to the non-static method getEndUserCharge(long, long, long, long) from the type UpdateUserWS" actually means.
The error is coming from:
public void updateDetailsPackage() {
some unrelated code
long zero=0;
double endUserCharge=0;
endUserCharge = UpdateUserWS.getEndUserCharge(long zero, long zero, long zero, long zero); <-------- error is here
[Code] ....
View Replies
View Related
May 26, 2015
Alright, I have two classes, this one
public class Player {
private String player;
public String getPlayer() {
return player;
}
private int strength;
private int defense;
[Code] .....
However, it says that under Player.getPlayer() that it 'Cannot make a static reference to the non-static method'.
View Replies
View Related
Oct 28, 2014
Why I can create an Instance of a class that contains non static variables within the static main method ?
This program runs fine
import java.util.*;
public class Test{
public int j;
public static void main(String[] args) {
Test test1=new Test();
System.out.println(test1.j);
[Code] .....
View Replies
View Related
Mar 15, 2015
I am trying to call an actionListener which is shown below in my PSVM :
class testMenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
getContentPane().removeAll();
createPanel();
getContentPane().add(panel1); //Adding to content pane, not to Frame
repaint();
printAll(getGraphics()); //Extort print all content
[Code] .....
I get the following error :
Frame.java:409: error: non-static variable this cannot be referenced from a static context
menuItem1.addActionListener(new testMenuItemListener());
View Replies
View Related
Mar 3, 2015
I am trying to add a field (called special) to a hibernate table. I am copying existing code (related to the NAME field) so I don't have to figure this out from scratch. I am getting the error
"[ERROR] C:VOXvoxware-1.1.13voxwarevoxware-implsrcmainjavacomvoxwareimplflowVoxFlowConfiguration.java:[213,38] error: non-static variable special cannot be referenced from a static context".
Line 213 is in public void mergeFrom, the actual line is "special = VoxFlowConfiguration.special;" I don't understand why Java thinks special is a "non-static" variable but it doesn't have a problem with the other variables (such as name, orderShow)
package com.voxware.impl.flow;
import com.voxware.asset.LiabilityType;
import com.voxware.flow.FlowConfiguration;
import com.voxware.flow.OrderFlow;
import com.voxware.flow.Step;
import com.voxware.i18n.LanguageCodes;
import com.voxware.impl.i18n.UTF8Control;
import com.voxware.impl.persistence.BaseEntity;
import com.voxware.impl.portal.VoxPortal;
[code]....
View Replies
View Related
Jul 27, 2014
I'm working on a banking program that is supposed to use 3 classes (Account-base class, CheckingAccount, and SavingsAccount) and several methods to display the banking information (ID, balance after a withdrawal and deposit, and the annual interest rate). This should be a pretty simple program, but I'm getting hung up on one portion of it. I'm getting some compiler errors, all of which deal with non-static variables being called from a static context (I'll also post these errors following the code). Up until the middle of last week, we just declared everything as static, but that's changed and I'm having trouble figuring out when to and when not to use static when declaring my methods, hence the compiler errors.
import java.util.Date;
public class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private Date dateCreated = new Date();
[Code] ....
Here are the compiler errors I am receiving:
Compilation completed. The following files were not compiled:
6 errors found:
File: C:UsersHiTechRedneckDesktopSummer II 2014Computer Programming PrincipleProgram 5CheckingAccount.java [line: 7]
Error: non-static method getId() cannot be referenced from a static context
[Code] .....
View Replies
View Related
Mar 14, 2015
I am trying to call an actionListener which is shown below in my PSVM :
class testMenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent arg0) {
getContentPane().removeAll();
createPanel();
getContentPane().add(panel1); //Adding to content pane, not to Frame
repaint();
[Code] .....
I get the following error :
Frame.java:409: error: non-static variable this cannot be referenced from a static context
menuItem1.addActionListener(new testMenuItemListener());
View Replies
View Related
Apr 27, 2014
I am writing the following program in Java SE 7. It throwing "Cannot make a static reference to the non-static type String" . However if I write parameterised String inside main method as java.lang.String[] args, it compiles fine.
class MainClass<String> {
<T> MainClass(T t) {
System.out.println(t.getClass().getName());
}
public static void main(String[] args) {
System.out.println("asdasd");
new MainClass<>("");
}
}
I mean following programs compile fine in Java SE 7 :
class MainClass<String> {
<T> MainClass(T t) {
System.out.println(t.getClass().getName());
}
public static void main(java.lang.String[] args) {
System.out.println("asdasd");
new MainClass<>("");
}
}
View Replies
View Related
Aug 19, 2014
i am trying to make something, and i want to request input from user and it shoud look like this
1. xxx -> press 1 to choose this
2. xxx -> press 2 to choose this
So if they enter 3 or string or whatever i want to restart method and show again
1. xxx -> press 1 to choose this
2. xxx -> press 2 to choose this
So here is method that i am trying to restart
public static void askUser(){
System.out.println("xxx");
System.out.println("1. xxx");
System.out.println("2. xxx");
System.out.println("3. xxx");
[code]....
If i try to make it public void than it say can't call non-static methods inside static(main).if i try to put it into new class and then call it after i fail input it goes into infinite loop.
View Replies
View Related