What does private MyShovel Shovel mean? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am having great difficulties understanding this construct
public class MyBucket extends bucket {
private double var1;
private MyShovel Shovel; <- this is the problem here
public MyBucket(int volume, MyShovel Shovel) {
Some code here
}
}
I cannot understand what private MyShovel Shovel is in Java terminology. It is a class defined in my project but I cannot determine what it is called in the Java world. Can someone please help me define it so I can research what it is. This is production code and it works. Just cannot figure out the official Java name for that construct.

Shovel is a (private) member. Its type is MyShovel. This means that every instance of MyBucket contains a reference to a MyShovel object, which can be referenced only from within the class, by the name Shovel.
Note that this declaration does not follow java naming conventions. In the proper conventions it would have been called myShovel, in lowerCamelCase.

Shovel is just a variable. It is an instance of MyShovel. MyShovel would be a Class that represents a virtual shovel. Since it is a global variable, or field, in MyBucket this means that each MyBucket also would have MyShovel inside of it.
In the code you provided it is undefined and has no value. Since it is private only methods in MyBucket can access it. The MyBucket constructor takes a MyShovel parameter and is most likely what Shovel is set to.
You can think of it like this:
Every MyBucket must have a MyShovel inside of it to make it work. So when creating a MyBucket you put a MyShovel inside of it. Shovel is the space that that particular MyShovel will be stored.
As others mentioned it should be named "shovel" for naming convention.

Related

Static methods or empty constructor [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
As someone who is learning Java I find it hard to pick whether I should create class with empty constructor or making all her methods statics.
If I’m having a class without properties that read files and doing operations on the data and Being called only one’s shout it be static or have empty constructor.
Because I need to call only one method from the class (and he calls the rest) should I make all methods static or should I call her by creating empty object ?
Actually it would be a private constructor, not empty as you don't want to instantiate the class. But here are a couple guidelines.
Create static methods that don't require accessing instance fields but do some computation. An excellent example of this is the Math.class
Instance methods are used when accessing instance fields and possibly changing then. Getters and setters are good examples. Sometimes private helper methods can be declared static.
But don't use static methods as the fundamental type or because they are easier to use (i.e. work in static or non-static context). They are contrary to the concept of OOP.

Is there a C and Python equivalent of Java's public and private? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I realized that in many languages I have learned includes the keywords public and private, and I also found out that Lua's equivalent to private is local which got me thinking if there is also an equivalent in C and Python.
So is there an actual equivalent of Java's public and private in C and Python?
There is a naming convention for protected and private fields in Python:
A prefix of one underscore means protected, two underscores mean private. But this is not really enforced. More details here:
https://www.tutorialsteacher.com/python/private-and-protected-access-modifiers-in-python
Everything not prefixed with one or two underscores is public.
In C, global variables and functions can be accessed from functions in other source files unless they are declared static. Not exactly the same as private, but C is not object-oriented so the concept of a class does not exist here.
In python you can declare private members by putting dunders(double underscore) before member name, like this :
class Sample:
def __init__(self):
self.__private_mem = "Can be accessed only by member functions"
self.public_mem = "Can be accessed as object properties outside the class"
sample = Sample()
print(sample.public_mem)
print(sample.__private_mem) # will raise an Error
But, I guess there is no such thing in C language as it is not object oriented.

In Java, if a method uses a static member, why should itself be declared as static? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I understand that in a static method, non-static members should be called with related objects, or there would be ambiguity.
If my understand is correct?
In Java, if a method uses a static member, why should itself be declared as static?
This is not true - a method that uses a static member does not need to be static itself.
I understand that in a static method, non-static members should be called with related objects, or there would be ambiguity.
If my understand is correct?
No.
When a member variable or method is static, it means that this member variable or method isn't part of, or doesn't work on one specific object of the class; it's shared by all objects of the class. The section Understanding Class Members in Oracle's Java Tutorials explains this in more detail.
Non-static methods work on a specific object, so if you call them from a static method, you have to call them on an object, since there is no current object (which this refers to) when you're in a static method.

why static methods and variables are accessible without any instance of class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Why is it that all static methods and variables are accessible without any instance of class and non-static members need instances to get access.
Actually, there is a class object representing each class in the JVM. So, the line Why is it that all static methods and variables are accessible without any instance of class is incorrect.
The JVM creates class objects (different from class instances) representing classes.
Example : String.class, Class.class etc
When We create a class that means we are creating 'Bunch of Objects(instances) of Same type',for those objects,methods are remains same but data and memory location changes and it is unique for each object.But When We use 'Static' variable or Method ,It creates Only ONE COMMON COPY in whole program.So it is same for all instances/objects and changes done in static method/variable is visible to all objects. hence,we can use it directly or without instance of class.in same way,non static members are different with respect to every object,so we need instance of class for it.

Why inner class can access to a private member of another inner class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I just found out that an inner class can access another inner class's private member like this:
public class TestOutter {
class TestInner1 {
private int mInt = 1;
}
class TestInner2 {
public int foo(TestInner1 value) {
return value.mInt;
}
}
}
Method foo of TestInner2 can access the private member mInt of TestInner1.
But I have never see this case before. I don't know the meaning of letting code in TestInner2 can access to the private member of TestInner1.
I was searched about inner class in google, none of the search results shows that inner class have this feature. I also look up to The Java Language Specification, but it still not mention about that.
"Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor." JLS 6.6.1 In this case, TestOutter is the top-level class, so all private fields inside it are visible.
Basically, the purpose of declaring a member private is to help ensure correctness by keeping other classes (subclasses or otherwise) from meddling with it. Since a top-level class is the Java compilation unit, the spec assumes that access within the same file has been appropriately managed.
This is because the inner class, as a member of the outer class, has access to all the private variables of its outer class. And since the other inner class is also a member of the outer class, all of its private variables are accessible as well.
Edit: Think of it like you have a couple of couch cushion forts(inner classes) in a house(outer class), one yours the other your siblings. Your forts are both in the house so you have access to all the things in your house. And mom(Java) is being totally lame and saying you have to share with your sibling because everything in the house is everyone elses and if you want your own fort you are going to have to buy it with your own money(make another class?).

Categories

Resources