On the line where I commented "ERROR HERE" I am getting an error stating "non static variable this cannot be referenced from a non-static context". But when I remove static from my methods, it runs and I get "Error: Main method is not static in class scrabble.Scrabble, please define the main method as:
public static void main(String[] args)". Not sure what is causing this.
package scrabble;
public class Scrabble {
class Tile{
int value;
char letter;
public Tile (char letter, int value) {
this.letter=letter;
this.value=value;
}
}
private static void printTile(Tile letter) {
System.out.print("Letter is: " + letter.letter);
System.out.println(" and the value is " + letter.value);
}
public static void testTile() {
Tile L1 = new Tile('z', 10); //ERROR HERE
printTile(L1);
}
public static void main(String[] args) {
testTile();
}
}
put the Title class in a separated file Title.java in the same package
package scrable
and make it public. that will work for you. :)
Related
public class testing {
public static void main(String[] args)
{
boolean a = true;
if (a) {
public static String word = " ";
}
else if (a == false) {
public static String word = "not";
}
System.out.println(word);
}
}
Instead of printing the value, it tells me "Illegal modifier for the variable word; only final is permitted.
I tried to use public static final String word = "not";
but I still got an error saying that it is wrong.
The variable should be created outside of your main:
public class Testing {
public static String word;
public static void main(String[] args)
{
boolean a = true;
if (a) {
word = " ";
} else {
word = "not";
}
System.out.println(word);
}
}
Alternatively you can create a variable inside you main as well.
public class Testing {
public static void main(String[] args)
{
boolean a = true;
String word;
if (a) {
word = " ";
} else {
word = "not";
}
System.out.println(word);
}
}
When variables, blocks or methods are made static, they are made available during load time. Rest all reserves memory during run time. All the local variables created inside a static method are present in the method stack, which as a package is already present during load time. So creating a static variable inside method, be it be static or non-static method, is not allowed.
My main java.
public class Main {
DecimalComparator dc = new DecimalComparator();
DecimalComparator rt = new DecimalComparator();
public static void main(String[] args) {
dc.areEqualByThreeDecimalPlace(1.256, 2.632);
}
}
My DecimalComparator code
public class DecimalComparator {
public void areEqualByThreeDecimalPlace(double num1,double num2) {
String Str1 = String.format("%.3g%n",num1);
System.out.println("The value of Str1"+Str1);
}
}
and in Min.class I have error "The method areEqualByThreeDecimalPlace(double, double) is undefined for the type DecimalComparator"
Your main method is static. You cannot get a non-static instance of DecimalComparator from a static method. You have to get an instance of your Main class to get an instance of your DecimalConverter class.
Field names in Java always start with a lower-case character.
Here's the complete runnable code I used. I made your DecimalComparator class an inner class so I could post the code as one block.
public class DecimalComparatorMain {
public static void main(String[] args) {
DecimalComparatorMain dcm = new DecimalComparatorMain();
DecimalComparator dc = dcm.new DecimalComparator();
dc.areEqualByThreeDecimalPlace(1.256, 2.632);
}
public class DecimalComparator {
public void areEqualByThreeDecimalPlace(double num1, double num2) {
String str1 = String.format("%.3g%n", num1);
System.out.println("The value of str1: " + str1);
}
}
}
I'm searching for modifier in Java that has the same exact purpose as Static in C++ has. I mean, that variable is only initialized once in function, then every time we call that function again, values from the previous call are saved. That's how code looks in C++:
void counter()
{
static int count=0;
cout << count++;
}
int main()
{
for(int i=0;i<5;i++)
{
counter();
}
}
and the output should be 0 1 2 3 4, is there something that has the same purpose, but in Java?
looks like you are starting with java. To help you understand the concept i wrote the same code with comments for your understanding.
package yourXYZpackage;
public class yourABCclass{
//Declare in class body so your methods(functions) can access
//and the changes made will be global in C++ context.
static int count=0;
void counter()
{
count = count++;
System.out.println(count);
//thats how you can display on console
}
//this is the main method like C++
public static void main(String[] args){
for(int i=0;i<5;i++)
{
counter();
}
}
}
hope this will help..//
u need to create a constructor first.
public class answer2 {
static int count =0;
answer2() {
System.out.println(count);
count++;
}
public static void main(String[]args) {
for(int i=0;i<5;i++) {
new answer2();
}
}
}
just define static variables as a class member normally. java developers promote object oriented programming so even your main function is a method defined in another class whose name is same as your program name.
now for your question if you want to define a static variable:
public class abc
{
public static int count = 0;
}
public class xyz
{
public void increment()
{
abc.count++;
}
public static void main(String[] args)
{
System.out.println(abc.count);
increment();
System.out.println(abc.count);
}
}
Hope it helps.
I feel as if this is super simple but I cant get this to work. I am trying to use this:
import java.util.Scanner;
import java.lang.Math;
public class SammysRentalPriceWithMethods
{
public static void main(String[] args)
{
Rental rental = new Rental();
SammysRentalPriceWithMethods SRPWM = new SammysRentalPriceWithMethods();
getLogo();
getContractNum();
getHoursAndMinutes();
}
public static void getLogo()
{
rental.setlogo();
}
public static void getContractNum()
{
rental.setContractNumber();
}
public static void getHoursAndMinutes()
{
rental.setHoursAndMinutes();
}
}
to call this class and the methods contained inside:
import java.util.Scanner;
import java.lang.Math;
public class Rental
{
public final int minutes = 60;
public final double hourlyRate = 40.0;
private static String contractNum;
private static double hours;
private static int minutes2;
private static double price;
Scanner Input = new Scanner(System.in);
public static void setlogo()
{
System.out.println("*********************************");
System.out.println("*Sammy's makes it fun in the sun*");
System.out.println("*********************************");
}
public static void setContractNumber()
{
Scanner Input = new Scanner(System.in);
System.out.println("Please enter your contract number.");
contractNum = Input.nextLine();
}
public static void setHoursAndMinutes()
{
int timeOver;
Scanner Input2 = new Scanner(System.in);
System.out.println("Please enter the amount of time in minutes you rented the equipment.");
minutes2 = Input2.nextInt();
if (minutes2 > 60)
{hours = (minutes2/60);
price = (hours * 40);
timeOver = (minutes2%60);
price = (price + timeOver);
System.out.println("You rented the equipment for " + hours + " hours and " + timeOver + " minutes.");
System.out.println("Your total price is: " + price);
}
else if (minutes2 < 60)
{
price = (minutes2 * 1);
System.out.println(price);
}
}
}
but the compiler is saying "error: cannot find symbol" on every reference of rental in the SRPWM class. I already called the class in the main method. Any ideas?
The compiler is right.
The scope of the variables rental and SRPWM is restricted to the main method.
Either you pass the attributes to the methods of the class or you make them static fields of SammysRentalPriceWithMethods.
Because rental is declared in your main method, it will only be visible in that method. You should consider declaring this variable at the class level.
You need to declare the Rental class variable outside the main() function. If you declare it inside the main(), then you won't be able to use it in other functions. So, make your Rental variable global.
New file should be this:
import java.util.Scanner;
import java.lang.Math;
public class SammysRentalPriceWithMethods {
Rental rental = new Rental();
public static void main(String[] args) {
SammysRentalPriceWithMethods SRPWM = new SammysRentalPriceWithMethods();
getLogo();
getContractNum();
getHoursAndMinutes();
}
public static void getLogo() {
rental.setlogo();
}
public static void getContractNum() {
rental.setContractNumber();
}
public static void getHoursAndMinutes() {
rental.setHoursAndMinutes();
}
}
The problem is you are mixing both static and non-static members and invoking them inside your SammysRentalPriceWithMethods class, so change the class as shown in the below code with comments:
public class SammysRentalPriceWithMethods {
private Rental rental;
//Use constructor to inject Rental object
public SammysRentalPriceWithMethods(Rental rental) {
this.rental = rental;
}
public static void main(String[] args) {
//create Rental object
Rental rental = new Rental();
SammysRentalPriceWithMethods srpwm =new SammysRentalPriceWithMethods();
//invoke methods using srpwm reference
srpwm.getLogo();
srpwm.getContractNum();
srpwm.getHoursAndMinutes();
}
public void getLogo() {
rental.setlogo();
}
public void getContractNum() {
rental.setContractNumber();
}
public void getHoursAndMinutes() {
rental.setHoursAndMinutes();
}
}
You need to remember the following basics:
(1) Call static members of a class using classname and . operator (if you want to call static members within static methods you call them without classname and .)
(2) Call non-static members using the object and . operator
(3) Name the variables, method names with camel case (starting letter in lower case)
Here's my code:
public class Oddmain {
public static void main(String[] args) {
}
private static void comp() {
for (int i = 1; i <= 10; i += 2) {
System.out.print(i + " ");
}
}
}
I want it to print out only odd numbers between 1 and 10, which it does fine. The thing I'm curious about is making it in another class, and calling upon it in the main method. How would I have the main method call upon something from another class?
I tried using the run method:
public void run();
//my for loop here
But that didn't really work. Whenever I move:
private static void comp() {
for (int i = 1; i <= 10; i += 2) {
System.out.print(i + " ");
}
}
to the other class, it says this:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method comp() is undefined for the type Oddmain
at Oddmain.main(Oddmain.java:4)
I think that this means it want me to create the method comp() in this class (Oddmain), but I want to create in the class Oddcomp. This is something I can do, correct?
I'm really new to coding, so please explain stuff very thorough when posting, so I know why I'm doing what I'm doing, and not just copy/pasting.
The problem that you have here it's that you don't close main method.
public class Oddmain {
public static void main(String[] args){
}
private static void comp() {
for(int i = 1; i <= 10; i += 2){
System.out.print(i + " ");
}
}
}
And now you can call your method comp from your main method:
public static void main(String[] args){
comp();
}
I expect it will be helpful for you!
public class Oddmain {
public static void main(String[] args){
private static void comp() {
for(int i = 1; i <= 10; i += 2){
System.out.print(i + " ");
}
}
}
There is no way this code works fine, you cannot create a new method with in the main statement. I believe you meant:
public class Oddmain {
public static void main(String[] args){
comp();
}
private static void comp() {
for(int i = 1; i <= 10; i += 2){
System.out.print(i + " ");
}
}
}
To utilise the comp method in another class you can go about this in two ways...
NOTE: In both cases you must change the access modifier of your method from private if you wish to use the method in another class.
Private fields can only be accessed with in the class they are defined in.
1) Create an instance of Oddmain in your other class.
public class OtherClass {
public static void main(String[] args){
Oddmain s1 = new Oddmain();
Oddmain.comp();
}
}
2) Call the comp() method in a static manner, static methods are often called by using the class name the static method is defined in, e.g.
Oddmain.comp();
Static methods should often be called via the latter format. In both cases you are calling the comp method in Oddmain. Remember static fields belong to a class not an object.