• Post Reply Bookmark Topic Watch Topic
  • New Topic

instance of the class to access a non-static class field  RSS feed

 
chris Dunbar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the main reason on what im trying to do.. i have a class that has a filed


and in the class InventoryEotInventory im trying to access public ItemStack[] inventory = new ItemStack[INV_SIZE];

someone told me

In Java and probably every language with classes, you need an instance of the class to access a non-static class field, because the field is different for every single class (that's the whole point). E.g.



Example a = new Example(1);
Example b = new Example(2);
System.out.println("A = " + a.value + " | B = " + b.value);


That's what I mean by 'you need an instance of the class' - both 'a' and 'b' are specific instances of Example, and thus each can have an independent value for 'this.value', whereas if you make 'value' static, the value will NOT be independent: 'a' and 'b' cannot have different values.

can anyone help .. i know java but i dont understand this...
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right. Instance variables are accessed through instance only. Each instance can have different values. This instances are created at run time
that's why you can have different values.

But in case of static variables, they are loaded at compile time. Static members are accessed through class(ex. ClassName.variableName). For each
instance same variable is shared and everyone sees same value.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your posts. Always use them, and a bit of space would make your code easier to read. We have some suggestions here, but even the little I have done makes it look a lot better

Remove the word public from every field in your classes. you should not have an array in one class which you can access like that from another class. Make all your variables private. You should replace that access with some method in the Inventory class which does all that work. I don't think it will compile because you haven't written return anywhere. Assuming inventory is a private field in the inventory class, you can have this method in the inventory classI can see several logical problems with that method still:-
  • 1: Do you really want to add to the List when the first element in the array is a tier 1 bag?
  • 2: You are probably using incorrect public access for the tier 1 bag, unless it is an enum element.
  • 3: Unless the tier 1 bag is an enum constant, the == operator is very likely to produce wrong results when you least expect it.
  • Yes, you need an instance to access a non‑static member of an object. Consider this Vehicle classThings static belong to the class not the object and there is always one copy of a static member. So there is one copy of speed. So all Vehicle objects see the same copy of speed, and however many (or few) objects you have, they will all have the same speed variable.
    ∴ All vehicles are travelling at the same speed
    So you delete the word static and vehicle1 can be travelling at a different speed from vehicle2.

    So, stop writing public when you should write private. And stop writing static unless you have a good explanation for doing so.
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i cannot add

    to my inventory class... the class that has

    extends AbstractTab, which that class i cannot edit because it is part of a api. So i have to get inventory from inside InventoryEotInventory to get it to work, i do not know how to do this because it will not work like


    because inventory is not static, so they sugested to do


    but i am very confused this

    if it helps any here is my whole 2 classes



     
    Guillermo Ishi
    Ranch Hand
    Posts: 789
    C++ Linux Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I agree the need for statics would be pretty rare. The best way to understand it is if a class C contains a static variable v, then however many instances of C there are there is still only one v. You can access it either as C.v as if there were no instances of C at all - there doesn't need to be to access a static variable in C, or you can access it as instanceName.v


    If v wasn't static, line 5 would be an error and the two instances would each have their own independent copy of v
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alright i understand but the way to acces a static is C.v them how am i sepost to access v if it is just public int v;
    I cannot do a C.v method
    Im trying to acces a non-static from another class



    I am trying to complete this but inventory is non static


    I cannot change it to static because then it will mess up other parts of classes that use it..

     
    Guillermo Ishi
    Ranch Hand
    Posts: 789
    C++ Linux Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If it isn't static then you have to create an instance and and access it through the instance variable created. I feel like you are using somebody else's code that you don't understand. You could take a mooc course that uses Java. Google moocs.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    chris Dunbar wrote:i cannot add
    . . .
    to my inventory class...
    . . .
    Of course you can. The fact that it extends another class does not stop you adding methods. Those methods would only be available if you declare the variable as inventory type rather than abstract tab. And if the superclass is really abstract, you must extend it before you try to instantiate it.
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ues but i cannot modify or change anything in the abstract class because that class is part of a api
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Cannot edit or change anything in tththe abstructtab class and i understand almost what you say but can you give examples visual learner
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    chris Dunbar wrote: . . . can you give examples visual learner
    Look in the 4th code block in this previous post of yours, line 42 and there you have exactly what I told you to write.

    In my first post, I pointed out three possible logic errors. Have you managed to look at them yet?
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You do not need to change anything in the abstract class.
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The shouldaddtolist is not a list it just tells the abstruxt class to mention it if it helps this is all the classes i use
    https://github.com/SlimeKnights/TinkersConstruct/tree/master/src/main/java/tconstruct/client/tabs
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have looked at my code everything in there is what it should be.. i am codding in forge (minecraft forge its a api ) and everything i have done has a reason to it. I added public bollian shouldaddtolist(){...... in my inventory class how am i sepost call it in the eottab that has shouldaddtolist
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok im am still a little confused now

     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What you have is a class with a field and a constructor. You will doubtless add methods to it.
    Line 3 shows 2 logic errors.
  • 1: Dangerous: You have declared the array as public, which means any code can alter it. Change it to private access.
  • 2: You have given the array an initial value which is going to be overwritten. Remove the assignment sign = and everything to the right of it. That is a minor problem.
  • Line 7 sets the array field to be the same object as the parameter. Line 8 prints the array; since arrays don't override the toString method, you will get an incomprehensible output looking rather like [LItemStack;@8d2a7c9f. If you want output you can understand try
    System.out.println(Arrays.toString(inventory));
    You don't usually put print statements into constructors. Constructors are there to initialise the object, and shouldn't be used for anything else. Maybe that line was only there for testing purposes.
    There is a dangerous vulnerability in line 7 which you probably have never been told about. You have an array in your class, but there may be other references to the same array elsewhere. Since they all point to the same object (I think this is called an alias), changes in one place are reflected in the other place. So if you write inventory[123] = new ItemStack(...); in one bit of code, you change inventory[123] everywhere else. One possible answer: clone the array. This will mean the array itself becomes a copy, but the elements are not copies, so they are still vulnerable to change elsewhere.
    this.inventory = inventory.clone();
    You have correctly worked out how to use this. in constructors .
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    so how am i sepost to call inventory in another class?
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why do you want the inventory in another class?
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Because i need to call it in another class to get something to work properly so the code in the other class basicly days if slot 0 is not null and has the item ... return true of not return false but after i took off static i could not do that so i need to access inventory to get it to work but since inventory isnt static i can not.

    I have tried to call it like A b = new A (); b.inventory........ but it just doesn't work that way so i am trying to call it another way
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Same way you would make any field available outside the class. With a getXXX method.We have already seen that simply returning an array is potentially a hazard, so let's enhance that method a bit.Now you can call the getInventory method and get a copy of the inventory. As I said before if the inventory items are mutable the other code can still change the state of those individual items.
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok so i called the class like this



    i get this error

    Unexpected error
    at com.mightydanp.eot.client.tabs.InventoryTabTier1Bag.shouldAddToList

     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No idea what that error means, I am afraid. The message is really informative, isn't it.
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i only seem to get this error whenever i try and use this method

    public InventoryEotInventory eotInventory = new InventoryEotInventory();


    when inventory was static this worked fine

    InventoryEotInventory.inventory[0] != null&& InventoryEotInventory.inventory[0].getItem() == ModItems.tier1Bag;
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If tier 1 bag is an enum element, then it can't be null.
    That means something to the left of the == sign and to the left of a dot . or a left square bracket [ must be null.
    So one of eot inventory, the result of the getInventory method, or its 1st element (= index 0) must be null. You should not get any difference in behaviour by getting rid of the if; I don't understand why you have got a more helpful error message this time.
    Try inserting this code:-See how many lines you get before it prints false. That will show you where the null is.
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i have just run that and i get this

    eotInventory: true
    eotInventory.getInventory(): false
    eotInventory.getInventory()[0]: false
    [11:09:36] [Client thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.client.event.GuiScreenEvent$InitGuiEvent$Post@4398d:
    java.lang.NullPointerException
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Go the the eotInventory class and get it to print out whether inventory is null. I think you will find it is.
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alright if thats whats causing the problem how to fix that
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You need to find inventory and find why it is null. Look in the constructor of that class.
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    this is what is inside there

    here is the class(have not edited anything just added )
    [spoiler]

    [/spoiler]
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I can see all sorts of things wrong, including
  • 1: Why have you got a 1‑element array?
  • 2: Why have you got two supposedly overridden methods which are empty?
  • But I can't see where inventory is null.

    Anybody else?
     
    chris Dunbar
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    if it helps you here is my github but dont freak out about the code

    https://github.com/mightydanp/EssenceofTech
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!