-2

OK, so I'm pretty sure I'm just missing something really dumb here, but for a coding assignment, I need to create a public class called 'Android' and give it instance variables and methods that meet the specifications.

I keep getting the error:

File: C:\Users\somerandomuser\Desktop\hmwk\Android.java [line: 35] Error: Cannot make a static reference to the non-static field tag

Here's my Android class:

import java.util.*;

public class Android
{
  private int tag;
  private String name;

  public Android()
  {
    name = "Bob"+tag;
    tag = changeTag();

  }

  public String getName()
  {
    return name;
  }

  private static boolean isPrime(int n)
  {
    //random magic bullshit goes here....
    int i;
    for (i=2; i<n;i++) {
      if (n%i==0)
        return false;
    }
    return true;
  }

  private static int changeTag()
  {
    //advanced magic bullshit goes here....
    boolean exit = false;
    int query = tag;
    while (!exit)
    {
      exit=isPrime(query);
      if (!exit)
      {
       query++;
      }
      else
      {
        int x;
      }
    }
  }
    public int getTag()
    {
      return tag;
    }
}

Thanks ahead of time!

Austen Clay
  • 39
  • 1
  • 8

1 Answers1

1

This is instance specific

private int tag;

This is in a static method.

private static int changeTag()
  {
    //advanced magic bullshit goes here....
    boolean exit = false;
    int query = tag;
    ...
 }

Make tag static, or change that method to non-static.

On a side note, the compiler did tell you to look at line 35 which is where that error occurred. Furthermore this is a homework exercise (/hmwk/) and stack overflow isn't a place for homework questions.

Bigminimus
  • 106
  • 3