Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Can you set a value to extended class?

    Hello everyone,
    I have a class called Config, now I'm extending another class called FileConfiguration on that Config class. You can make instances of FileConfiguration and my class Config you can also make instances of it. Normally, I have an instance of FileConfiguration in my Config class, this instance is changed to different values in different methods for different jobs in my Config class. That instance of FileConfiguration is set to other instances of FileConfiguration in different methods. I have a method that returns that specific instance of FileConfiguration. I want to get rid of the method and the instance of FileConfiguration in my config class, but then extend FileConfiguration. Now, how do I set the EXTENDED FileConfiguration to other instances of FileConfiguration like I usually do? I think that I shouldn't use this = other instances of FileConfiguration, then how do I do it?

    Regards
    augustas656

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Can you set a value to extended class?

    Can you post a small sample piece of code showing your problem?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you set a value to extended class?

    I'll write an example

    Java Code:
    public class Config {
        private FileConfiguration config = null;
        
        //This class has Constructor
        //Constructor doesn't initialise the instance of FileConfiguration called "config"
        
        public void example1() {
        config = example2().loadConfiguration(); //Method that returns a FileConfiguration class
        config.example3(); //Method that saves FileConfiguration to a specified folder if not there already
        }
        
        public FileConfiguration get() {
        return config;
        }
    }
    And I can do in main class:

    Java Code:
    Config cfg = new Config(arg1, arg2, arg3 etc);
    cfg.saveDefault(); //Just a method I need to do, this method doesn't exist in FileConfiguration Class
    cfg.get().getString("example");
    
    //or
    
    Config cfg = new Config(arg1, arg2, arg3 etc);
    cfg.saveDefault(); //Just a method I need to do, this method doesn't exist in FileConfiguration Class
    filecfg = cfg.get();
    filecfg.getString("example");
    I want to change it to something like:

    Java Code:
    public class Config extends FileConfiguration {
        
        //This class has Constructor
        
        public void example1() {
        this = example2().loadConfiguration; //This doesn't work...
        this.example3(); //Doesn't do what I want to do, it does it on the whole Config instance rather than the extended FileConfiguration
        }
    }
    So I should be able to do:

    Java Code:
    Config cfg = new Config(arg1, arg2, arg3 etc);
    cfg.saveDefault();
    cfg.getString("example");
    This is crude so it may have errors etc...
    Basically I want to change it from the first two to the second two, except I don't know how, the way I showed it doesn't work...

    Regards
    Augustas
    Last edited by augustas656; 09-14-2014 at 08:23 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Can you set a value to extended class?

    You should read about the this variable:
    Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    If you want to set the contents of an object, create some constructors to do it.

    public class Config extends FileConfiguration
    That seems backwards to me. The following makes more sense:
    public class FileConfiguration extends Config
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you set a value to extended class?

    FileConfiguration is in an API I'm using, I can't change it because my java program won't work otherwise... FileConfiguration is an abstract class too... so...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Can you set a value to extended class?

    What is the problem with the last 3 lines of code show in your post?

    Java Code:
    Config cfg = new Config(arg1, arg2, arg3 etc);
    cfg.saveDefault();
    cfg.getString("example");
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you set a value to extended class?

    No that's what I'd like to be able do, instead of the "And I can do in main class: " code snippet.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Can you set a value to extended class?

    What error messages do you get with that code? It looks like it could be ok.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you set a value to extended class?

    No it's not about that, the function getString() exists in super classes of my Config class. I don't think I'll be able to do what I intend, I can send you the API, it's not mine, I'm talking about Minecraft's Bukkit API btw. You may or may not know it, anyway...

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Can you set a value to extended class?

    Try to make an example of the problem and is close to compiling. The one that was posted had too many problems.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you set a value to extended class?

    I was only showing an example without all the actual code.. Okay, firstly a question, if you extend a Class, are you creating a new instance of just JUST getting all the methods to your class? If you are creating an instance, what I wanted to do, is change that instance to another instance... That's all...

    EDIT: I really want to keep my code private, but I think it'd be so much easier if I would show it, can I message you or?
    Last edited by augustas656; 09-14-2014 at 10:42 PM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Can you set a value to extended class?

    creating an instance, what I wanted to do, is change that instance to another instance.
    You can change the contents of an instance:
    Java Code:
      Aclass ac = new Aclass();
      ac.changeContents();  // change the contents of the one instance
    or create a new instance
    Java Code:
      Aclass ac = new Aclass(args1...);
      ac = new Aclass(args2...);  //  new instance with different contents
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you set a value to extended class?

    I want to keep my code private, but I'm stuck and can't move further, can I msg you the code and explain or? It would be so much easier, but as I've said, I want to keep my code private mostly...

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Can you set a value to extended class?

    want to keep my code private
    Then make a small, simple example for posting.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you set a value to extended class?

    Nevermind, I'll just post the whole thing as I believe it's easiest to explain my problem, this is so slow, these are not the only forums I'm on, mainly I'm on the forums closest related to my problem, mainly for the API of what I'm basing my work on. Eventhough I don't have a lot, I still don't want ppl to exactly copy me. Making a small simple example is what I did earlier in this thread.

    Core.java
    Java Code:
    public class Core extends JavaPlugin {
    	public static Plugin plugin;
    	public static PluginDescriptionFile pluginInfo;
    	
    	public static String pluginName;
    	public static String pluginVersion;
    	public static String pluginTitle;
    	
    	public static File dir_data;
    	public static Config config;
    	
    	@Override
    	public void onEnable() {
    		plugin = this;
    		pluginInfo = getDescription();
    		pluginName = pluginInfo.getName();
    		pluginVersion = "v" + pluginInfo.getVersion();
    		pluginTitle = pluginName + " " + pluginVersion;
    		
    		dir_data = getDataFolder();
    		if (!dir_data.exists()) {
    			dir_data.mkdirs();
    			
    		}
    		
    		config = new Config("config", true);
    		
    		Writer.load();
    		Writer.sendConsole("Enabled " + pluginTitle, "success");
    		
    	}
    	
    	@Override
    	public void onDisable() {
    		Writer.sendConsole("Disabled " + pluginTitle, "success");
    		
    	}
    	
    }
    Config.java
    Java Code:
    package plugin.craftworks;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.UnsupportedEncodingException;
    import java.util.logging.Level;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;
    
    public class Config {
    	private static Plugin plugin;
    	private FileConfiguration config = null;
    	private File dir, file = null;
    	private String name = null;
    	
    	public Config(String configName, File directory, boolean saveDefault) {
    		plugin = Core.plugin;
    		name = configName + ".yml"; dir = directory;
    		file = new File(dir.getAbsoluteFile() + File.separator + name);
    		if (saveDefault) { saveDefault(); }
    		
    	}
    	
    	public Config(String configName, boolean saveDefault) {
    		plugin = Core.plugin;
    		name = configName + ".yml"; dir = plugin.getDataFolder();
    		file = new File(dir.getAbsoluteFile() + File.separator + name);
    		if (saveDefault) { saveDefault(); }
    		
    	}
    	
    	public void reloadConfig() {
    		if (file == null) { file = new File(dir, name); }
    		
    		config = YamlConfiguration.loadConfiguration(file);
    		
    		Reader stream = null;
    		
    		try {
    			stream = new InputStreamReader(plugin.getResource(name), "UTF8");
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		}
    		
    		if (stream != null) {
    			config.setDefaults(YamlConfiguration.loadConfiguration(stream));
    			
    		}
    	
    	}
    	
    	public FileConfiguration getConfig() {
    		if (config == null) { reloadConfig(); }
    		return config;
    		
    	}
    	
    	public void saveConfig() {
    		if (config == null || file == null) { return; }
    		
    		try { getConfig().save(file);
    		} catch (IOException ex) {
    			plugin.getLogger().log(Level.SEVERE, "Could not save config to " + file, ex);
    			
    		}
    		
    	}
    	
    	public void saveDefault() {
    		if (file == null) { file = new File(dir, name); }
    		if (!dir.exists()) { dir.mkdirs(); }
    		if (!file.exists()) { plugin.saveResource(name, false); }
    		
    	}
    	
    }
    I have one more class called Writer, but it's not important or related

    Okay, BOTH of these Java codes work perfectly fine I get no errors etc. I use the getConfig() function to get FileConfiguration that's loaded and then I use metods from FileConfiguration to get variables. I want to extend FileConfiguration instead of having an instace of it inside my code and doing so REPLACE the private FileConfiguration config and where it's used. But in reloadConfig() method I use config = YamlConfiguration.loadConfiguration(file); I can't use this = YamlConfiguration.loadConfiguration(file); If extending classes creates instances of them, then I want to complete CHANGE those instances to YamlConfiguration.loadConfiguration(file), else, If extending classes doesn't create instances of those extended classes, I want to create one, as I already have with my working Core.java and Config.java classes but instead of having to use getConfig() and ontop of that only THEN using methods from the FileConfiguration and it's super classes I want to directly use those methods in FileConfiguration. So I thought I should EXTEND, that's where all this came from. The main problem here is config = YamlConfiguration.loadConfiguration(file); I want to be able to change the instance created as the extension to my class, to YamlConfiguration.loadConfiguration(file).

    It's so hard to explain anyway, and this is the API btw, all of it: https://github.com/Bukkit/Bukkit/tre...ava/org/bukkit

    Regards
    augustas656

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Can you set a value to extended class?

    Sorry, I would rather work with a small example. Most of the code is not part of the problem.
    Try to make a small, simple example that shows the problem. It should only require a few methods and a few variables.

    Making a small simple example is what I did earlier in this thread.
    I've already said that the code in the example looked ok.

    The code that tried to assign a value to the this variable made no sense.
    See post#12 for 2 ways to change the instance that a variable refers to.
    Last edited by Norm; 09-15-2014 at 04:38 AM.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you set a value to extended class?

    Does extending a class create a new instance of the class that you're extending?

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Can you set a value to extended class?

    Quote Originally Posted by augustas656 View Post
    Does extending a class create a new instance of the class that you're extending?
    If D extends B and you create a new D, memory is allocated for an object D which consists of all members of B plus the members defined in D. The constructor for D sets the latter members after the constructor for B has set the 'B part' of the newly created object.
    Build a wall around Donald Trump; I'll pay for it.

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Can you set a value to extended class?

    Quote Originally Posted by augustas656 View Post
    Does extending a class create a new instance of the class that you're extending?
    I think you're getting confused by how some other more scripting oriented language works. Extending a class is basically declaring a new class and adopting the common features of an existing class. The point being that you are still just declaring a new class.

    Be careful with your terminology as you can easily confuse yourself and others. An instance of a class is an object.

    Java Code:
    Book harryPotter = new Book("Harry Potter"); // <--- the harryPotter variable refers to an instance of the class Book
    Last edited by gimbal2; 09-15-2014 at 05:43 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  20. #20
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you set a value to extended class?

    NOTE: My java project I export to .jar, as what I'm creating exists in jar form, non-executable jar.

    I've come to a conclusion that to solve my problem the only way is reading the API I'm using to find where everything is linked and where everything begins, because it just keeps going to different classes, implementing, extending from one to another, and I get so confused on the way.

    There's a class FileConfiguration: https://github.com/Bukkit/Bukkit/blo...iguration.java
    FileConfiguration extends MemoryConfiguration: https://github.com/Bukkit/Bukkit/blo...iguration.java
    MemoryConfiguration implements Configuration: https://github.com/Bukkit/Bukkit/blo...iguration.java
    MemoryConfiguration extends MemorySection: https://github.com/Bukkit/Bukkit/blo...rySection.java
    MemorySection implements ConfigurationSection: https://github.com/Bukkit/Bukkit/blo...onSection.java

    In my source folder and main package I have a class called: Config
    The class is like a Config Loader, it stores information about a single configuration, it stores it's path, it's name, itself in readable form (FileConfiguration) etc.
    This class has a FileConfiguration instance called config and in the class outside all other methods and constructors it's initiated with null.
    The constructors in the Config class only set some values to variables and optionally it copies a config from the JAR (this java project) to the data folder (folder outside JAR).

    I have a method in my Config class called saveDefault which copies the default version of the config file from the JAR if not there already.
    I have a method in my Config class called reloadConfig which loads the config saved in the data folder wether it is or isn't modified.
    The reloadConfig method, a.k.a loadConfig, initiates the instance of FileConfiguration that is set to null or earlier version of an earlier loaded config.
    By using this method and initiating the instance of FileConfiguration you are reading the config saved in the data folder and making the instance a readable version of the config.

    I have a method in my Config class called getConfig which returns the instance of FileConfiguration, so I can get Strings and other variables with methods.
    If the instance of FileConfiguration is null, then before returning the instance, the instance is loaded from the data folder by calling reloadConfig.

    Instead of having to use getConfig() method and then read from the instance of FileConfiguration that is returned, I wanted to extend FileConfiguration so I didn't have to have a getConfig() and I could literally merge the abilities to load configs and read configs. However, what do I replace or what do I do with the instance of FileConfiguration now that I don't need it? If I were to use a method like getString() it would return errors, because they're trying to read nothing, I haven't loaded the FileConfiguration, instead of initiating that instance of FileConfiguration how do I initiate the Config class itself to be able to read what it has loaded itself. This is WHY I need to find what I need to do in these classes in my API to be able to change this.

    Regards
    augustas656

Page 1 of 2 12 LastLast

Similar Threads

  1. Stumped by Json, file I/O, extended class
    By Digital Larry in forum New To Java
    Replies: 9
    Last Post: 05-11-2013, 06:13 AM
  2. @RunWith(Parameterized.class) extended class error
    By Richard3 in forum New To Java
    Replies: 5
    Last Post: 01-31-2012, 02:47 PM
  3. Replies: 1
    Last Post: 07-21-2011, 10:46 PM
  4. Extended Class - Cannot Find Symbol
    By javak in forum New To Java
    Replies: 15
    Last Post: 02-27-2011, 04:43 PM
  5. Returned type changes if class is extended. How?
    By Gelembjuk in forum New To Java
    Replies: 10
    Last Post: 10-31-2008, 11:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •