2

it might be a stupid question, but I'm getting confused here. I've the following situation:

Main.java

public class Main {

    public static void main (String args[]){
        GenericTag[] arr = new GenericTag[2];
        arr[0] = new Authentication("", "", "", "");
        arr[1] = new Document("", "", "", "");
        byte[] foo= Base64.decodeBase64(XmlBuilder.generate(arr));
        System.out.println(new String(foo));
    }

XmlBuilder.java

public final class XmlBuilder {
    private static final String OPEN_TAG = "";
    private static final String CLOSE_TAG = "";

    public static byte[] generate(GenericTag[] tags){

        String xml = OPEN_TAG;
        for(int i=0; i<tags.length; i++){
            xml += tags[i].xml;
        }
        xml += CLOSE_TAG;

        return Base64.encodeBase64(xml.getBytes());
    }

    public interface GenericTag{
         public String getXml();
    }

    public class Authentication implements GenericTag{
        private static final String OPEN_TAG = "<authentication>";
        private static final String CLOSE_TAG = "</autentication>";
        //some tags

        public Authentication (/*some parameters*/){
            xml = OPEN_TAG;
            //xml building
            xml += CLOSE_TAG;
        }

        @Override
        public String getXml() {
            return xml;
        }
    }

    public class Document implements GenericTag{
        private static final String OPEN_TAG = "<document>";
        private static final String CLOSE_TAG = "</document>";
        //some tags

        public String xml;

        public Documento (/*some params*/){
            xml = OPEN_TAG;
            //xml building
            xml += CLOSE_TAG;
        }
    @Override
    public String getXml() {
        return xml;
    }
    }
}

I can't get it to work. The compiler says that both Authentication and Document cannot be resolved to a type. If I explicitly state new XmlBuilder.Authentication it says

No enclosing instance of type XmlBuilder is accessible. Must qualify the allocation with an enclosing instance of type XmlBuilder (e.g. x.new A() where x is an instance of XmlBuilder).

What am I doing wrong here?

jack_the_beast
  • 1,838
  • 4
  • 34
  • 67
  • 1
    Are you sure you meant to have Authentication as an inner class of XmlBuilder? If not you should declare each interface/class in its own file. – OH GOD SPIDERS Jun 16 '17 at 08:20
  • yes I know it putting each class in its own file would solve averything, but I want to have everything in a single class in this case. – jack_the_beast Jun 16 '17 at 08:30

3 Answers3

5

Make classes Authentication and Document public static. Since those are not static, you can instantiate them only from XmlBuilder instance.

Java inner class and static nested class - you can find more information here

esin88
  • 3,091
  • 30
  • 35
0

You try to make an instance of inner classes of abstract class. Make this classes independent.

Kutti
  • 486
  • 1
  • 6
  • 17
  • yes I know it putting each class in its own file would solve averything, but I want to have everything in a single class in this case. thank you anyway – jack_the_beast Jun 16 '17 at 08:31
0

First, you have some syntax errors, second, you need to make the inner classes static so that they don't rely on an outer instance but stand on their own. You need to refactor them to not rely on any fields of the parent class then. Better style would be to have them in their own source file, but in one file, a compiling version of your code would be:

import java.util.Base64;

public abstract class XmlBuilder {
    private static final String OPEN_TAG = "";
    private static final String CLOSE_TAG = "";
    private String xml;

    public static byte[] generate(GenericTag[] tags){

        String xml = OPEN_TAG;
        for(int i=0; i<tags.length; i++){
            xml += tags[i].getXml();
        }
        xml += CLOSE_TAG;

        return Base64.getEncoder().encode(xml.getBytes());
    }

    public static interface GenericTag{
        public String getXml();
    }

    public static class Authentication implements GenericTag{
        private static final String OPEN_TAG = "<authentication>";
        private static final String CLOSE_TAG = "</autentication>";
        private static String xml;
        //some tags

        public Authentication (/*some parameters*/){
            xml = OPEN_TAG;
            //xml building
            xml += CLOSE_TAG;
        }

        @Override
        public String getXml() {
            return xml;
        }
    }

    public static class Document implements GenericTag{
        private static final String OPEN_TAG = "<document>";
        private static final String CLOSE_TAG = "</document>";
        //some tags

        public String xml;

        public Document (/*some params*/){
            xml = OPEN_TAG;
            //xml building
            xml += CLOSE_TAG;
        }
    @Override
    public String getXml() {
        return xml;
    }
    }
}

And the Main class:

import java.util.Base64;

public class Main {

    public static void main(String[] args)
    {
        XmlBuilder.GenericTag[] arr = new XmlBuilder.GenericTag[2];
        arr[0] = new XmlBuilder.Authentication();
        arr[1] = new XmlBuilder.Document();
        byte[] foo= Base64.getDecoder().decode(XmlBuilder.generate(arr));
        System.out.println(new String(foo));
    }

}
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118