I've been working with Java in the last few days, so I am very new to programming in Java.
I am currently going through a bunch of online tutorials and trying to learn as much as possible. In one tutorial, we are getting to learn how to use objects correctly. The person who made the video uses an online compiler, while i follow along in eclipse.
In my code I have two classes
The first is called objectDesign
public class objectDesign {
public static void main(String[] args) {
System.out.println("We are creating a new PEZ dispenser");
PezDispenser dispenser = new PezDispenser();
System.out.printf("The dispenser is %s", dispenser.characterName);
}
}
The second is called PezDispenser
public class PezDispenser {
public String characterName;
public static void main(String[] args) {
String characterName="Mario";
}
}
The goal is to define the character who the object is supposed to be. I used a string called characterName and set it to Mario which i would like to return when I run the objectDesign class. I made the string public thinking that would enable the objectDesign class to find the information. However the console returns "The dispenser is null" every time I run the code.
What am I doing wrong?
You can use below code
package com.stackoverflow;
public class ObjectDesign {
public static void main(String[] args) {
System.out.println("We are creating a new PEZ dispenser");
PezDispenser dispenser = new PezDispenser("Mario");
System.out.printf("The dispenser is %s", dispenser.characterName);
}
}
package com.stackoverflow;
public class PezDispenser {
public String characterName;
public PezDispenser(String characterName) {
// TODO Auto-generated constructor stub
this.characterName=characterName;
}
}
here are few ways of doing this
Option 1:
public class PezDispenser {
public String characterName = "Mario";
public static void main(String[] args) {
String characterName="Mario"; // This statement is never executed because this class's main was never invoked.
}
}
Option 2:
public class PezDispenser {
public String characterName;
public static void main(String[] args) {
String characterName="Mario";
}
}
public class objectDesign {
public static void main(String[] args) {
System.out.println("We are creating a new PEZ dispenser");
PezDispenser dispenser = new PezDispenser();
dispenser.characterName = "Mario";
System.out.printf("The dispenser is %s", dispenser.characterName);
}
}
These are not the best options, but going by what you are trying to achieve these should serve the purpose.
Related
In a small example, I'm trying to take information from the String "helloFromMain" in a file called Main.java and move it "outside" the public static void into a public static string in a different file named data.java.
IN THE FILE MAIN.JAVA
public class Main {
public static void main(String[] args){
String helloFromMain = "hello";
}
}
IN THE FILE DATA.JAVA
public class Data {
public static String helloFromData = helloFromMain;
}
Any help would be appreciated.
Also im relatively new to all this
You can set a public static variable from another class.
public class Main {
public static void main(String[] args){
String helloFromMain = "hello";
Data.helloFromData = helloFromMain;
}
}
Also, I've found that it's helpful to set a package for all classes, as it makes it simpler to manage importing and FS structure.
You can do it like this
Simple way.
IN THE FILE MAIN.JAVA
public class Main {
public static String helloFromMain; //declare a public static string, it will be accessible from outside the class.
public static void main(String[] args){
helloFromMain = "hello";
}
}
IN THE FILE DATA.JAVA
public class Data {
public static String helloFromData = Main.helloFromMain;
}
But be aware that Main.helloFromMain will be null until you call the main constructor.
Advance way :
IN THE FILE MAIN.JAVA
public class Main {
private static String helloFromMain; //This time the static variable is private, so you can't directly use it from outside
public static void main(String[] args){
helloFromMain = "hello";
}
//We create a public static method to access the private static variable
public static String getHelloFromMain(){
return helloFromMain;
}
}
IN THE FILE DATA.JAVA
public class Data {
//We call our public static method from Main.
public static String helloFromData = Main.getHelloFromMain();
}
You can create a static method in class Data to set the value of the string. If you do this, you don't need to make the variable in class Data public.
public class Main {
public static void main(String[] args) {
String helloFromMain = "hello";
Data.setHelloFromData(helloFromMain);
}
}
public class Data {
private static String helloFromData;
public static void setHelloFromData(String newValue) {
helloFromData = newValue;
}
}
I'm reading the types of error in Code Smells and trying to understand in the category Inappropriate Intimacy,
I found the solution move field. Can someone exemplify and explain me that solution "Move field"?
I found something in https:refactoring.guru/move-field but I still do not understand.
For example field MOVE_ME is used only in different class (MyRealUssage). so you can move it:
public class Unused {
public String moveMe = "This is used only in other classes"
}
public class MyRealUssage {
public static void main(String[] args) {
System.out.println(new Unused().moveMe);
}
}
Move field will update valid field location:
public class Unused {
}
public class MyRealUssage {
public String moveMe = "This is used only in other classes";
public static void main(String[] args) {
System.out.println(new MyRealUssage().moveMe);
}
}
Obviously a better code will also removing unused Unused and use getter for field,as
public class MyRealUssage {
public String moveMe = "This is now used only this class";
public static void main(String[] args) {
System.out.println(getMoveMe());
}
public String getMoveMe() {
return moveMe;
}
}
public class Main {
public static void main(String[] args) {
**how i call the function here** ?
}//public
public static void nameAndAge() {
System.out.println("Name: Rubik Seviyants Age: 27");
}
}//main
package com.pervacio.adminportal.tradein.constants;
public class Main {
public static void main(String[] args) {
nameAndAge();
}//public
public static void nameAndAge() {
System.out.println("Name: Rubik Seviyants Age: 27");
}
}//main
Please read this for better understanding: Java Methods | Tutorials Point
If the function that has to be called is in the same class then just call the function like this:
function_name(parameters);
But if the function is in another class then you will have to create an object of the class and call:
Class_name object_name=new Class_name(); object_name.function_name();
These are the ways to call the function :
public class Main {
public static void main(String[] args) {
nameAndAge();
// or
Main.nameAndAge();
// or
Main s=new Main();
s.nameAndAge();
} //public
public static void nameAndAge() {
System.out.println("Name: Rubik Seviyants Age: 27");
}
}
public class Test {
public static void main(String[] args) {
System.out.println(Hello.a1);
}
}
class Hello {
static final int a1=10;
static {
System.out.println("SB");
}
}
This code is always printing 10 but not printing SB.Why?
A static final field is implemented as a compile-time constant which is replicated in the accessing method/class without any reference to the context of definition (its class). This is why accessing it does not trigger the static class block.
This is basically due to the final keyword. If you remove final as follows:
public class Test {
public static void main(String[] args) {
System.out.println(Hello.a1);
}
}
class Hello{
static int a1=10;
static{
System.out.println("SB");
}
}
You will see that SB is printed as expected.
You are using static final constant variable. This variable will be replaced at the compile time by the actual constant value, in-order to increase the performance. If you have a look at the compiled binary code (Yes you cannot ;) but technically speaking with an assumption) it will be something similar to this:
public class Test {
public static void main(String[] args) {
System.out.println(10); // Constant
}
}
class Hello {
static final int a1=10;
static {
System.out.println("SB");
}
}
Based on this code, class Hello will not be loaded into the RAM. So it will not print the SB.
Your static code block willbe invoked, when you create an instance of the class. Try this:
public class Test {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(Hello.a1);
}
}
class Hello {
static final int a1 = 10;
static {
System.out.println("SB");
}
}
I tried making a simple class in java using netbeans IDE. Whenever I try to do execute this it gives such warning."non static variable referenced from static context".Can anyone tell me why it happens and how to solve it. Thanx in advance.
public class HW3Q4 {
class Payment{
private double amount_payment;
public void set_amount(double amount){
amount_payment = amount;
}
public double get_amount(){
return amount_payment;
}
public void paymentDetails(){
System.out.println("The amount of the payment is: "+amount_payment);
}
}
public static void main(String[] args) {
// TODO code application logic here
Payment p1 = new Payment();
p1.set_amount(34000.00);
p1.get_amount();
p1.paymentDetails();
}
}
You make a mistake in creating the object. So this would help you:
public class HW3Q4 {
class Payment{
private double amount_payment;
public void set_amount(double amount){
amount_payment = amount;
}
public double get_amount(){
return amount_payment;
}
public void paymentDetails(){
System.out.println("The amount of the payment is: "+amount_payment);
}
}
public static void main(String[] args) {
// TODO code application logic here
HW3Q4 newInstance = new HW3Q4();
newInstance.init();
}
public void init(){
Payment p1 = new Payment();
p1.set_amount(34000.00);
p1.get_amount();
p1.paymentDetails();
}
}
Your payment class is within the HW3Q4 which try to act similar to say a string field within your class HW3Q4 like private String myString. So in order to avoid the error use:
HW3Q4 h = new HW3Q4 ();
Payment p1 = h.new Payment();
You are declaring a separate Payment class for each instance of HW3Q4. Instead, I think you want to declare Payment in its own file (Payment.java), which would be preferred, but I guess you could declare it as a static inner class - just change class Payment { /* ... */ } to static class Payment { /* ... */ }.