I am trying to access a private final static double from another class.
Here is the class:
public class coolMath{
private final static double alpha = 5.87;
public coolMath(){
}
public static double calDistance(double x1, double y1, double x2, double y2){
double dist = Math.sqrt(Math.pow((x2-x1), 2) + Math.pow((y2-y1),2));
return dist;
}
}
I need to access the variable alpha in another class. Is this possible? Does something need to happen in the constructor to make it available? Any ideas?
Either make alpha a public field or provide a public static double getAlpha() that returns it.
If you make the field public, you access it like so double a = coolMath.alpha.
Otherwise, double a = coolMath.getAlpha();
I strongly suggest you go through java modifiers again.
This could technically be possible through some esoteric reflection methods, but I highly discourage it. It's better to change the visibility of your alpha variable or write some method that returns it (or perhaps sets it, if you need that also (but not if your var is final)).
Related
I am doing a project for CS, and I just realized that I do not understand the mechanics behind the this. java reference and getters. Specifically, if I have the following:
class Circle{
private int radius;
}
public Circle(int radius){
this.radius = radius;
}
public int getRadius(){
return radius;
}
Why is it that for the constructor, I use this.radius to reference the data field "radius" in the Circle class, but for the constructor, I have this.radius = radius?
Does it make a difference whether or not I use the this. so long as it is the only data field named radius?
I just tested it on Sublime, and it outputs the same result.
Just according to my own logic, would it not make more sense to use this.radius to return the radius in the getRadius() getter instead of just return radius because I am referring to the data field in the object Circle?
I really appreciate all the help I can get!
It's because radius is the name of both parameter of constructor and field of the class. To disambiguate those this keyword is used. In case of getter this is not needed, but also won't hurt. Some formatters add this by default, it's equivalent to:
public int getRadius(){
return this.radius;
}
You don't need this in the constructor if you do not shadow the local name. That is with
public Circle(int r){
this.radius = r;
}
You can write
public Circle(int r){
radius = r;
}
The this is only required when it is used to specify which radius you are referring to.
Actually when you refer to this.radius that means you use a class field variable. Otherwise (in your code) you may re-assign your radius as an argument in a given constructor that is may be unwanted in your case. To distinguish it you must either use different name of a variable or use this.
I have trouble understanding why double xx and yy are put final in the constructor. Why don't I just put them double xx and double yy. Why
do they have to be final? I guess the whole purpose of this is
creating an immutable object.
public class Point {
private final double x, y;
private double distance;
public Point(final double xx, final double yy) {
this.x = xx;
this.y = yy;
this.distance = -1;
}
}
There is no need for these parameters to be final.
There are two reasons to make parameters final:
To make use of them in an inner class declared in that function;
To prevent their values from being changed accidentally.
Clearly (1) doesn't apply.
(2) isn't necessary because it's such simple code, and you can see that it's not changing the parameters.
There is a school of thought which says that all parameters and local variables should be declared final as a matter of course, as it makes it easier to reason about the code, in the same way that using immutable types makes it easier to reason about code using them.
There is another school of thought which says that adding final everywhere is just unnecessary noise, and, if you are writing methods where you can't tell if the value is changing, your methods are too long.
Largely, making parameters and local variables final comes down to personal/team preference.
Declaring x and y variables as final solves the purpose (i.e. The value must not be changed later). There is no point in declaring the constructor's parameters as final.
Looking to use one variable to define an x1, x2, x3, x4 for a method. Is there a type that exists that would allow me to do this? I could just do 2 Points, but I'd rather just have one variable. If one doesn't exist, am I able to create a type similar to this that I can make useable in all classes in my package?
Thanks!
Have you considered defining your own tuple?
public class PointsTuple
{
public Point Point1;
public Point Point2;
// replace with appropriate setters and getters if you want.
}
Hopefully you will give your class a more descriptive name than PointsTuple but you get the idea. Here I used public fields but if you are familiar with encapsulation you may opt to use set & get methods.
Yes! The rectangle!
java.awt.rectangle
Rectangle(int x, int y, int width, int height)
Example:
Rectangle r = new Rectangle(x1, y1, x1-x2, y1-y2)
Try making a custom class like this:
class MyPoint{
private int x1;
private int x2;
private int x3;
private int x4;
//Setters and Getters for the points
}
I am an AP java student and while working on a project I wondered if it is a good practice to have a parameter that is a global variable. If you're wondering why I would want to do that well is so I wouldn't have to do this:
public class Circle {
private DrawingTool pen;
private SketchPad paper;
private double myX;
private double myY;
private double myWidth;
private double myHeight;
public Circle(double x, double y, double width, double height){
paper = new SketchPad(500,500);
pen = new DrawingTool(paper);
x = myX; //I don't want to have to assign this every time
y = myY; //like here
width = myWidth; // and here
height = myHeight; // and here
}
}
is it allowed to just do the following:
public Circle(double myX, double myY, double myWidth, double myHeight){
paper = new SketchPad(500,500);
pen = new DrawingTool(paper);
}
}
and every time I pass the arguments to the parameter they will automatically be assigned to the global variables?
and every time I pass the arguments to the parameter they will automatically be assigned to the global variables?
No. There's nothing within Java which will make the "parameter to instance variable" (which isn't really "global") assignment automatic. Many IDEs have the ability to generate the code for you, but it does need to be there.
An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.
Source: What Is an Object?
How public members of a class causes havoc in java? Can someone please explain with example? I tried to create such situation but couldn't succeed. I simply found them equivalent to 'protected' access modifier.
It allows invalid values, breaking encapsulation.
public class Clock {
public int hours;
public int minutes;
}
Then, in unrelated code...
Clock clock = new Clock();
clock.hours = 42;
clock.minutes = 99;
Having them private with setter and getter methods allows encapsulation to enforce proper values.
public class Clock {
private int hours;
private int minutes;
public void setHours(int hours) {
if (hours < 0 || hours > 23) throw new IllegalArgumentException("bad range");
this.hours = hours;
}
// Likewise for "setMinutes" method.
}
Here's a tutorial page on encapsulation in Java on encapsulation's benefits. Quoting:
The fields of a class can be made read-only or write-only.
A class can have total control over what is stored in its fields.
The users of a class do not know how the class stores its data. A
class can change the data type of a field, and users of the class do
not need to change any of their code.
I believe it all depends on the application/program that you design.
Declaring the members as private definitely does have advantages.
But on the other hand,
If you design say a Point Class, which the users would be inheriting
and using it to draw various shapes, square, rectangle, circle, you
might think of keeping the memebers x, y, z as public.
Example:
class Point{
public double x = 0.0;
public double y = 0.0;
public double z = 0.0;
}
The advantage here would be; the classes Rectangle, Square, can access the points directly
say;
class Square extends Point{
private Point p;
p.x = 4.0;
p.y = 10.0;
p.z = 0;
// Instead of using double x = p.getX(); double p.setX(5.0);
}
Hope this helps.
Read the below articles; it should help you.
Source 1
Source 2
Source 3