class Pname1 is public, should be declared in a file named Pname1 - java

public class Pname {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a+b=" + (a + b));
System.out.println("Hello World!");
}
}
public class Pname1 {
public static void main(String[] args) {
private String myName = "sha";
private String myHname = "pra";
System.out.println("the adding together is=" + (myName + myHname));
}
}
public class Pname2 {
public static void main(String[] args) {}
}

Create three different java files, Pname.java, Pname1.java and Pname2.java and place your corresponding public classes in these files. In java, one .java source code file can contain at most one public class.

Related

Is there a way to take a string from inside a public static void and move it "outside" into a new class into a public static string?

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;
}
}

Whats the best pratice to create constant variables and subclasses inhertience them

I am not sure if there is a standard or good practice to create the constant variables in the parent class, and then subclasses reuse.
public class parentA{
protected static final String name = Constants.SYS_NAME;
protected static final String code = Constants.CODE;
}
public class son extends parentA{
private static void main(String[] args)
{
System.out.println("Name: "+name);
System.out.println("Code: "+code );
}
}
A common practice is to use an interface for constants.
interface MyInterface {
public static final int X = 10;
public static final int Y = 20;
}
public class Main {
public static void main(String[] args) {
System.out.println(MyInterface.X);
}
}
Output:
10

I have an attributes class where i set and get hero attributes and heroes class where i set heroes info and i want the game to process in my main

This is my class Attributes where setting and getting the variables is done
class Attributes{
private int heroHp, heroDamage, heroArmor, currentHp, maxHp, rng,
playerAction;
private String heroName;
private boolean isAlive, run;
public void setName(String name){
heroName = name;
}
public String getName(){
return heroName;
}
public void setHp(int hp){
heroHp = hp;
maxHp = heroHp;
}
public int getHp(){
return heroHp;
}
public void setDamage(int damage){
heroDamage = damage;
}
public int getDamage(){
return heroDamage;
}
public void setArmor(int armor){
heroArmor = armor;
}
public int getArmor(){
return heroArmor;
}
public void setIsAlive(boolean isAlive){
this.isAlive = isAlive;
}
public boolean getIsAlive(){
return isAlive;
}
public void displayAttributes(){
System.out.println("====================================");
System.out.println("Hero name: " + heroName);
System.out.println("Health: " + heroHp);
System.out.println("Damage: " + heroDamage);
System.out.println("Armor: " + heroArmor);
System.out.println("====================================\n");
}
This is my Heroes class
class Heroes{
public static void main(String[] args){
Attributes slardar = new Attributes();
Attributes phantomAssassin = new Attributes();
slardar.setName("Slardar");
slardar.setHp(115);
slardar.setDamage(14);
slardar.setArmor(6);
slardar.setIsAlive(true);
phantomAssassin.setName("Phantom Assasin");
phantomAssassin.setHp(90);
phantomAssassin.setDamage(17);
phantomAssassin.setArmor(8);
phantomAssassin.setIsAlive(true);
}
}
How do i call or use those created objects in my main class? I tried but i really don't know. I also tried doing this
public class Main{
public static void main(String[] args){
Attributes attribs = new Attributes();
Heroes heroes = new Heroes();
heroes.slardar.displayAttributes();
}
}
You are having 2 main methods please try to use only one. As per your Main class heroes.slardar().displayAttributes(); says slardar() is a static method in Hero class , which is not present there.
Instead you can code like this :
class Heroes {
public static Attributes getSlardar() {
Attributes slardar = new Attributes();
slardar.setName("Slardar");
slardar.setHp(115);
slardar.setDamage(14);
slardar.setArmor(6);
slardar.setIsAlive(true);
return slardar;
}
}
public class Main {
public static void main(String[] args) {
Attributes attribs = new Attributes();
Heroes heroes = new Heroes();
heroes.getSlardar().displayAttributes();
}
}

Advantage between "import static class.*;" over multiple static "class.method()" call(s)?

Is there a runtime advantage to importing static methods vs calling the static class.method() as needed?
Example...
package com;
public class TestMain {
public static void main(String[] args) {
TestOne firstTester = new TestOne();
TestTwo secondTester = new TestTwo();
firstTester.setVars();
firstTester.setVarSum();
firstTester.printVarSum();
secondTester.setVars();
secondTester.setVarSum();
secondTester.printVarSum();
}
}
package com;
public abstract class TestSubMethods {
public static int getMethodOne(){return 1;}
public static int getMethodTwo(){return 2;}
public static int getMethodThree(){return 3;}
public static int getMethodFour(){return 4;}
}
-------------------------------------------------------------------
package com;
public class TestOne {
private int varOne;
private int varTwo;
private int varThree;
private int varFour;
private int varSum;
public void setVars() {
this.varOne = TestSubMethods.getMethodOne();
this.varTwo = TestSubMethods.getMethodTwo();
this.varThree = TestSubMethods.getMethodThree();
this.varFour = TestSubMethods.getMethodFour();
}
public void setVarSum() {
this.varSum = this.varOne + this.varTwo + this.varThree + this.varFour;
}
public void printVarSum() {
System.out.println("varSum = " + this.varSum);
}
}
-----vs-----
package com;
import static com.TestSubMethods.*;
public class TestTwo {
private int varOne;
private int varTwo;
private int varThree;
private int varFour;
private int varSum;
public void setVars() {
this.varOne = getMethodOne();
this.varTwo = getMethodTwo();
this.varThree = getMethodThree();
this.varFour = getMethodFour();
}
public void setVarSum() {
this.varSum = this.varOne + this.varTwo + this.varThree + this.varFour;
}
public void printVarSum() {
System.out.println("varSum = " + this.varSum);
}
}
Between "TestOne" and "TestTwo" Is there a preferred style? Is one a faster runtime than the other? I was going to use "extends TestSubMethods", but it was recommended I don't do this.
Only difference I feel is static import decreases readability.
When static import is used, one cannot tell where this method exists.
public void setVars() {
this.varOne = getMethodOne(); // static method from current class
this.varTwo = getMethodTwo(); // static method imported from other class
}
But at the same time while writing test cases,
Assert.assertEquals(1, 1); // not preferred
assertEquals(1, 1); // prferable
That's because we are habitual of it, and we don't get confuse where assertEquals exists.

Can't send string to another class from if loop

I'm trying to pass a string from one class to another, but not succeeding. I realized during research and trial and error that I need to have "public static void main(String[] args) {}" to be able to use the if statement, but then getY() produces an error. What can I do differently?
public class Testing {
public static String z;
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
public static String getZ() {
return z;
}
}
}
The other class is
class B {
public static void main(String args[]) {
String x = Klasatest2.getZ();
System.out.println(x);
}
}
Error:
Klasatest2.java:14: illegal start of expression
public static String getZ()
^
Klasatest2.java:14: illegal start of expression
public static String getZ() {
^
Klasatest2.java:14: ';' expected
public static String getZ() {
^
Klasatest2.java:14: ';' expected
public static String getZ() {
^
4 errors
For starters, you can't declare a method inside of a method,
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
public static String getZ() {
return z;
}
}
}
So you have to make sure that get getZ() method is declared OUTSIDE of main(string[] args)
Like this,
public class Test {
public static String z;
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
}
public static String getZ() {
return z;
}
}
Also, you shouldn't have two main(String[] args) methods, as only one of them will be called unless for some reason you decide to call it yourself, which would be very strange.
So if you wanted the string to be set in class Test, you would need to call it's main method from your other class, possible like this.
Test.main(null);
Your application can only have one main(String args[]) method.
Try this:
public class Testing {
public static void main(String[] args) {
A a = new A("hy");
B b = new B(a.z);
}
public class A {
public String z;
public A (String z) {
this.z = z;
}
}
public class B {
public B (String y) {
System.out.println(y);
}
}
}

Categories

Resources